About fgetc() in C

64 Views Asked by At

The code paragraph is as follow:

fp_t=fopen(temp_name,"r");
int j=0; 
do
{
    char ch=fgetc(fp_t);
    if(feof(fp_t)==0) all[i].note[j]=ch;
    j++;
} while(feof(fp_t)==0);
all[i].note[j-2]='\0';
fclose(fp_t);

The problem is about line all[i].note[j-2]='\0';. When running this code, the content in the file fp_t can be well-absorbed. But why it should be j-2 when add '\0'?

I tried to change it as j-1, and if I print it to stdout, an '\n' is appended after the string. I confirmed that there is no '\n' in the original string.

0

There are 0 best solutions below