我试图找出文件指针在遍历文件时如何移动。为此,我编写了这段代码 -
#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");
}
}
现在,除了 3 个地方之外,这个 O/P 是可以理解的 -
输入第一行时,为什么第 14 位的指针值出现两次?文件不应该在 EOF 第一次出现时结束吗? 14.
为什么会发生这种情况?输入第二行后,为什么指针的第15位不见了?
为什么第 16 个字符后有一行空?第 17 个字符不应该出现在没有空行的下一行吗?