[英]writing into a file in c
i have an char
array b[20]
which i want to write into a file . 我有一个char
数组b[20]
我想写入一个文件。 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 . 在每次迭代之后, b[20]
的值发生变化,所以我想在每次迭代中在文件的每一行中写入字符串。 So how can i change a new line in a file and 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);
}
假设char数组包含以null结尾的字符串:
fprintf(file, "%.20s\n", b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.