0

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

#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 是可以理解的 -

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

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

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

4

2 回答 2

0

在你使用它们之前,你必须知道一些东西。

#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. 第一点应该解释一下。干杯。
于 2016-04-18T13:59:54.967 回答
0

你要这个:

#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返回EOF你忽略的。

另请参阅此问题

顺便提一句

  • 我不确定您实际期望什么输出。
  • 第二个while循环与第一个相同,你应该把它放在一个函数中。
于 2016-04-18T13:51:21.820 回答