#include<stdio.h>

int main()
{
    FILE* f;
    f=fopen("book.txt","w");
    char* sentence="0123456789";
    fprintf(f,"%s\n",sentence);
    fseek(f,0,SEEK_END);
    int a=ftell(f);
    printf("%d\n",a);
    fclose(f);
    return 0;
}

I have the code above which prints out 12 when I run it. why is it not 11 (0,1,2,3,4,5,6,7,8,9,\0) instead of 12?

EDITED: (0,1,2,3,4,5,6,7,8,9,\r\n)

有帮助吗?

解决方案

On Windows systems, the newline is actually two characters: Carriage-return and the newline characters ("\r\n").

So you have your ten characters from the string you write out, plus the two for the newline.

其他提示

Running on Windows, perhaps? On OS X and Linux, this prints 11 for me.

If you are on a windows system, you are printing two newline characters: #13 and #10 by adding an extra \n. Remove it ad see what you get:

fprintf(f,"%s",sentence);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top