开发者

how to add new line at the end of file. using Only C

I am wondering how to add the new line before closing the file.

I have tried using fputs and puts and frpints something like 开发者_开发问答puts("/n"); etc but it doesnt work.

Thanks & regards, SamPrat


a very simple way, no error checking:

FILE * file = fopen(fname, "a");
fwrite("\n", strlen("\n"), 1, file);
fclose(file);


You should use "\n" instead of "/n" with the file opened in "appending mode" (letter 'a' as fopen parameter


Open the file with append flag "a" and then use fputs() function.


The strings "\n" and "/n" are very different. The first has 1 character (plus a null terminator); the second has 2 characters (plus a null terminator).

The character used for line termination is '\n'. puts() appends one such character automatically.

The following statements do the same thing (they may return a different value, but that isn't used in the example below):

printf("full line\n");
fputs("full line\n", stdout);
puts("full line");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜