문제

#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