开发者

writing into a file in c

开发者 https://www.devze.com 2022-12-27 20:01 出处:网络
i have an char array b[20] which i want to write into a file. After every iteration the value of b[20] changes , so i would like to write the string in each line of the file in each iteration . So how

i have an char array b[20] which i want to write into a file . After every iteration the value of b[20] changes , so i would like to write the string in each line of the file in each iteration . So how can i change a new line in a file a开发者_如何学Gond also put in a array of character?


Something like:

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); 
}


Assuming the char array contains a null-terminated string:

fprintf(file, "%.20s\n", b);
0

精彩评论

暂无评论...
验证码 换一张
取 消