写入c中的文件

Hic*_*ick 2 c

我有一个char数组b[20],我想写入一个文件.每次迭代后都会b[20]发生更改的值,所以我想在每次迭代中在文件的每一行中写入字符串.那么如何更改文件中的新行并放入一个字符数组呢?

cod*_*ict 7

就像是:

FILE *fp = fopen("file.txt","w");
// check for error
char b[20];

while(some_condition) {

 // populate the char array b and terminate it with NULL char.

 // write to file...and write a newline.
 fprintf(fp,"%s\n",b); 
}
Run Code Online (Sandbox Code Playgroud)

  • 另外,不要忘记循环后的`fclose(fp)` (2认同)