Pregunta

#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)

¿Fue útil?

Solució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.

Otros consejos

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);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top