I am trying to find how the file pointer moves while traversing the file. For that purpose, I have written this piece of code -
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
fp=fopen("example.txt","w+");
fputs("This is a test",fp);
printf("The initial text - \n");
int x=0; // For the purpose of debugging
rewind(fp);
while(!feof(fp))
{
char ch=getc(fp);
printf("File pointer - %d and letter - ",ftell(fp));
if(ch=='\t')
puts("tab");
else
if(ch=='newline')
puts("\n");
else
putchar(ch);
printf("\n");
}
fputs("\nThis is the second line",fp);
printf("\n\nThe final text - \n");
rewind(fp);
while(!feof(fp))
{
char ch=getc(fp);
printf("File pointer - %d and letter - ",ftell(fp));
if(ch=='\t')
puts("tab");
else
if(ch=='\n')
puts("newline");
else
putchar(ch);
printf("\n");
}
}
Now, the O/P for this is understandable except for 3 places -
When the first line is inputted, why is the pointer value for the 14th position present twice? Isn't the file supposed to end at the first occurrence of the EOF - 14.
Why does this happen?After the second line is inputted, why is the 15th position of the pointer missing?
Why is there a line empty after the 16th character? Isn't the 17th character supposed to occur on the next line itself without an empty line?