0

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 -

  1. 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?

  2. After the second line is inputted, why is the 15th position of the pointer missing?

  3. 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?

Martin James
  • 24,453
  • 3
  • 36
  • 60
Getsuga
  • 13
  • 4
  • Your compiler should be spitting out warnings, don't ignore them. – Unimportant Apr 18 '16 at 13:42
  • 3
    Possibly related: ['Why is “while ( !feof (file) )” always wrong?'](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – molbdnilo Apr 18 '16 at 13:42
  • 1
    Just as a friendly reminder: `int fgets(FILE *stream)` <= There's a reason for the `int`, here. – dhke Apr 18 '16 at 13:43
  • 1
    Also, posting the output you're referring to wouldn't hurt. – molbdnilo Apr 18 '16 at 13:47
  • 1
    Please don't edit the code in your question - it causes confusion and invalidates comments/answers. Also, copy/paste in the source you built and tested, ie. source that compiles. Posting code that does not compile gives the impression that you have not actually built and tested it at all and have just transcribed it from printed homework:( If you want to show changes that you made to assist with debugging, APPEND the new code underneath, with suitable comments, or ask another question. – Martin James Apr 18 '16 at 13:58

2 Answers2

0

You want this:

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

Now the output should what you expect.

Your problem was following: you read a charactewr with getc, but you dont test if it's an EOF (end of file). The while (feof(...)) is useless, because once you have read the last char of the file, feof still is false, therefore you enter the loop again, and this time getc returns EOF which you ignore.

See also this question.

BTW

  • I'm not sure what output you actually expect.
  • the second while loop is identical to the first one, you should put that in a function.
Community
  • 1
  • 1
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

There are some stuff that you had to know before you even used them.

#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. You need to check whether the character is actually an EOF In a file even EOF is considered as a character to represent the end. the feof function assumes that also to be a character and hence passes that loop. there you get a junk
  2. puts actually inserts a newline at the end when you use it. To avoid it use fputs instead
  3. First point should explain it. Cheers.
sameera sy
  • 1,708
  • 13
  • 19