0

I am doing a very simple homework that requires me to output a file's content, but I unexpectedly reach EOF before doing anything. The file contains just the word "pig" and for some reason EOF returns 16. I am using Dev-Cpp and the program is in C.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp;
    fp=fopen("C:/Users/myusername/Desktop/numeri.txt", "r");
    char c;
    if (fp!=NULL)
    {
        printf ("File opened correctly.\n");
        c=fgetc(fp);
        printf("%d\n", feof(fp)); //FEOF EQUALS 16 FOR SOME REASON
        while (feof(fp)==0)
        {
            putchar(c);
            c=fgetc(fp);
        }
        fclose(fp);
        system("PAUSE");
        return 0;
    }
    else
    {
        printf ("File cannot be opened.\n");
        system("PAUSE");
        exit(1);
    }
    system("PAUSE");
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 3
    Firstly `char c` should be `int c`, note that `fgetc` returns `int` and `putchar` takes `int`. Secondly please read [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong]) – Weather Vane May 22 '17 at 09:57
  • 1
    Also do not use `system("pause");` (see [system(“pause”); - Why is it wrong?](http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong) ) – Ilya May 22 '17 at 10:00
  • 1
    `int c; while((c = fgetc(fp)) != EOF) { putchar(c); }` – Weather Vane May 22 '17 at 10:01
  • Still not working. – randomdood1923 May 22 '17 at 10:05
  • 1
    [Difference between int and char in getchar/fgetc and putchar/fputc?](http://stackoverflow.com/q/35356322/995714) – phuclv May 22 '17 at 10:07
  • You problem statement "EOF returns 16" is unclear. If you are still using `feof`, remove all traces of it from the code. It does have a use, but not here. – Weather Vane May 22 '17 at 10:12
  • I've tested "c" 's value, it's -1. How do I fix? Btw, thanks for the replies, expecially for the one explaining why system("pause") is bad, I'll keep it in mind. – randomdood1923 May 22 '17 at 10:13
  • 1
    That is the value of `EOF`, -1. It does need "fixing", just understanding. This is why `c` must be type `int`. – Weather Vane May 22 '17 at 10:14
  • I've removed feof. Now the problem is that "c" equals -1. Before you ask, I've checked the directory that I specified in the program many times so that isn't the problem. – randomdood1923 May 22 '17 at 10:15
  • Please read my previous comment, and re-read all of them. – Weather Vane May 22 '17 at 10:16
  • So, now c is an int. EOF is given if we reach end of the file, right? But my file isn't an empty file, it contains the word "pig". Now either everything I've been taught about files is false or I'm not seeing something. – randomdood1923 May 22 '17 at 10:16
  • If `EOF` is the return value from the *first* `fgetc` call, then the file is not the one you think it is, it is empty. Have you flushed the output buffer? `fflush(stdout);` – Weather Vane May 22 '17 at 10:17
  • alright..but now it's only printing "ig". – randomdood1923 May 22 '17 at 10:22
  • I am reading your code by remote vision. You still have `c=fgetc(fp);` before the revised loop `while((c = fgetc(fp)) != EOF) { putchar(c); }` so the "p" is not getting printed. Think carefully about what you are doing. – Weather Vane May 22 '17 at 10:25
  • when responding to an error indication (that is returned from a system function) 1) the error message should be output to `stderr`, not `stdout` 2) should also output the reason the system thinks the error occurred. So this statement: `printf("File cannot be opened.\n");` should be: `perror ("File cannot be opened.\n");` – user3629249 May 22 '17 at 15:47

1 Answers1

0

per the man page for feof():

The function feof() tests the end-of-file indicator
for the stream pointed to by stream, returning nonzero if it is set.

So the only expectation is the returned value is 0 or non-zero.

So there is nothing wrong with the value 16

here is a proposed version of the code:

  1. it cleanly compiles
  2. it performs the desired function
  3. it implements the comments to the question
  4. readability and understanding is enhanced by separating code blocks (for, if, else, while, do...while, switch, case, default) via a single blank line.

and now the code

#include <stdio.h>   // fopen(), printf(), perror(). fclose(), fgetc()
#include <stdlib.h>  // exit(), EXIT_FAILURE

int main( void )
{
    FILE *fp = fopen("big.txt", "r");

    if (fp!=NULL)
    {
        printf ("File opened correctly.\n");

        int c=fgetc(fp);

        while (EOF != c)
        {
            putchar(c);
            c=fgetc(fp);
        }

        fclose(fp);
        return 0;
    }

    else
    {
        perror ("File cannot be opened.\n");
        exit( EXIT_FAILURE );
    }
}

Note: the file: big.txt just contains big

here is the resulting output:

File opened correctly.
big
user3629249
  • 16,402
  • 1
  • 16
  • 17