C将文件替换为程序内的字符串

时间:2014-12-15 11:29:17

标签: c string

我有一个从文件xxx.conf

读取数据配置的程序
FILE *file = fopen("xxx.cof", "rb");

然后数据以

给出
char line[128];
while(fgets(line, sizeof(line), file))

我想将文件xxx.conf替换为字符串文件=" name = xxxx \ nsurname = xxx \ n adress = xxx"

我希望程序从字符串中读取数据。

相反

FILE *file = fopen("xxx.conf", "rb"):

file = "name = xxx\nsurnam =xxx\nadress = xxx"

1 个答案:

答案 0 :(得分:1)

如果我理解你的问题,你应该使用strtok

  char line[] = "name = xxx\nsurnam =xxx\nadress = xxx";

  char *entry;
  entry = strtok(line, "\n");

  while (entry)
    {
      printf("%s\n", entry);
      entry = strtok(NULL, "\n");
    }

只需用代码替换printf