search

Home  >  Q&A  >  body text

linux - 怎样将printf()函数打印出来的内容存在一个字符数组中?

PHP中文网PHP中文网2818 days ago1079

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:01:10

    1. The following example code stores dynamic content in buf, and then writes the content in buf to a file.
    2. In addition to using sprintf, you can also use snprintf, which is buffer safe. sprintf has the risk of buffer overflow.
    3. The specific manual can be viewed using man sprintf under Linux.

    #include <stdio.h>
    
    int main()
    {
        char buf[1024] = {0};
        char * name = "acb0y";
        char * addr = "guangdong";
        sprintf(buf, "name=%s,addr=%s", name, addr);
        
        printf("buf=[%s]\n", buf);
        return 0;
    }
    

    reply
    0
  • Cancelreply