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

さて、この O/P は 3 か所を除いて理解できます -

  1. 最初の行を入力すると、14 番目の位置のポインター値が 2 回表示されるのはなぜですか? ファイルは EOF - 14 の最初の発生で終了するはずではあり
    ません。これはなぜですか?

  2. 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);
    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ですEOFwhile (feof(...))ファイルの最後の文字を読み取ってもまだfalseであるため、これは役に立たないため、再びfeofループに入り、今回は無視するものをgetc返します。EOF

この質問も参照してください。

ところで

  • あなたが実際に期待する出力がわかりません。
  • 2 番目の while ループは最初のループと同じです。それを関数に入れる必要があります。
于 2016-04-18T13:51:21.820 に答える
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 その文字が実際にIn a file でもEOF終わりを表す文字と見なされるかどうかを確認する必要があります。feof関数はそれも文字であると想定しているため、そのループを通過します。そこにあなたはがらくたを手に入れます
  2. puts実際に使用すると、最後に改行が挿入されます。それを回避するには、fputs代わりに使用します
  3. 最初のポイントはそれを説明する必要があります。乾杯。
于 2016-04-18T13:59:54.967 に答える