C中文本文件中的文件指针位置

File Pointer locations in a text file in C

我试图找出文件指针在遍历文件时是如何移动的。 为此,我编写了这段代码 -

#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");
    }
}

现在,O/P 是可以理解的,除了 3 个地方 -

  1. 输入第一行时,为什么第14位的指针值出现了两次? 文件不应该在第一次出现 EOF - 14 时结束吗?
    为什么会这样?

  2. 输入第二行后,为什么指针的第15位不见了?

  3. 为什么第16个字符后有一行是空的? 第 17 个字符不是应该在没有空行的情况下出现在下一行吗?

你想要这个:

#include<stdio.h>
#include<conio.h>

void main()
{
    FILE *fp;
    fp=fopen("example.txt","w+");
    fputs("This is a test",fp);
    int x=0;                                     // For the purpose of debugging
    rewind(fp);
    while(1)                  // changement here
    {
        int ch=getc(fp);      // changement here
        if (ch == EOF)        // changement here
          break;
        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            putchar(ch);
        printf("\n");
    }
    fputs("\nThis is the second line",fp);
    printf("\n\nThe final text - \n");
    rewind(fp);
    while(1)
    {
        int ch=getc(fp);   // changement here    
        if (ch == EOF)     // changement here
          break;

        printf("File pointer  - %d and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            putchar(ch);
        printf("\n");
    }
}

现在输出应该符合您的预期。

您的问题如下:您使用 getc 读取了一个字符,但您没有测试它是否为 EOF(文件结尾)。 while (feof(...)) 没有用,因为一旦你读取了文件的最后一个字符,feof 仍然是假的,因此你再次进入循环,这次 getc returns EOF 你忽略了。

另见 this question

顺便说一句

  • 我不确定您实际期望的输出是什么。
  • 第二个 while 循环与第一个相同,您应该将其放在一个函数中。

有些东西在您使用之前必须了解。

#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);
        if(ch==EOF){
            break;
        }
        printf("File pointer  - %ld and letter - ",ftell(fp));
        if(ch==EOF){
            break;}
        if(ch=='\t')
            puts("tab");
        else
        if(ch=='\n')
            puts("newline");
        else
            printf("%c",ch);
        printf("\n");
    }
    fputs("\nThis is the second line",fp);
    printf("\n\nThe final text - \n");
    rewind(fp);
    while(!feof(fp))
    {
        int ch=getc(fp);
        if(ch==EOF){
            break;
        }
        printf("File pointer  - %ld and letter - ",ftell(fp));
        if(ch=='\t')
            puts("tab");
        else

        if(ch=='\n')
            //printf("newline");
            //fputs(input, stdout);
            fputs("newline",stdout);
        else
            putc(ch,stdout);
        printf("\n");
    }
}
  1. 你需要检查这个角色是否真的是 EOF 在文件中,甚至 EOF 也被视为表示结束的字符。 feof 函数假定它也是一个字符,因此传递了该循环。你得到了一个垃圾
  2. puts实际上是在你使用的时候在末尾插入一个换行符。为避免它使用 fputs 而不是
  3. 第一点应该解释一下。 干杯。