0

I know this is an easy problem but i can't figure out how to solve this. I was learning about file and when i tried to read a file, it went on in an infinite loop. Here is my code:

int main()
{

    FILE *p;

    p = fopen("123.txt", "r");

    char ar[150];   

    while(!feof(p))
    { 
        fgets(ar, 150, p); 
        puts(ar);
    }

    fclose(p);

    return 0;
}
Thuan Nguyen
  • 431
  • 6
  • 18
  • See https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong?s=1|227.2797 – hat Jul 03 '18 at 14:58

2 Answers2

3

You need this:

#include <stdio.h>

int main()
{
  FILE *p = fopen("data.txt11", "r");
  if (p == NULL)
  {
    printf("File could not be opened.\n");
    return 1;
  }

  char ar[150];

  while (fgets(ar, 150, p) != NULL)
  {
    puts(ar);
  }

  fclose(p);
  return 0;
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • I have tried this but it doesn't work. It didn't go into an infinite loop but i didn't print out anything – Thuan Nguyen Jul 03 '18 at 15:00
  • 1
    @ThuanNguyen that would be a different question, but is close to impossible. Only "close to" because you might start this from an IDE and just not see the output, or your text file could be empty. Note the solution in this answer will add extra newlines. –  Jul 03 '18 at 15:05
  • 1
    @ThuanNguyen probably the file could not be opened, I edited my answer, try now. – Jabberwocky Jul 03 '18 at 15:07
  • The file can't be opened. Thank you very much. I understand it now – Thuan Nguyen Jul 03 '18 at 15:09
  • @FelixPalmen it's probably not a dupe of [this](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) but a dupe of the question: "File X exists but fopen cannot open it" or something like this. – Jabberwocky Jul 03 '18 at 15:09
  • @ThuanNguyen but [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) still stands – Jabberwocky Jul 03 '18 at 15:11
  • @Jabberwocky what do you mean? I am new to stackoverflow, did i do something wrong? I know that this is a duplicate question, what can i do to solve this? – Thuan Nguyen Jul 03 '18 at 15:12
  • @Jabberwocky indeed, "infinite loop" with the code shown indicates a different problem ... is there a dupe for "why does this code without any error checking not work?" ;) –  Jul 03 '18 at 15:13
  • 1
    @ThuanNguyen it's all fine, the dupe just doesn't explain what you experience, but still applies to the question. Your code is broken because of the wrong use of `feof()` **and** because of not checking for **any** errors. –  Jul 03 '18 at 15:14
  • 1
    @ThuanNguyen no you did nothing wrong while asking your question. And it's not really a duplicate of the question mentioned. What I mean is that checking for end of file as you did it is wrong. – Jabberwocky Jul 03 '18 at 15:14
2

Don't use feof(), see Why is “while ( !feof (file) )” always wrong? .

Check the return value of fopen() whether fopen() was success or failed, if fopen() returns NULL then don't proceed further or don't do any operation with p. for e.g

FILE *p = fopen("data.txt11", "r");
if (p == NULL) {
    fprintf(stderr,"file doesn't exist\n");
    return 0;
}

And check the return value of fgets(). From the manual page of fgets()

RETURN VALUE fgets() return s on success, and NULL on error or when end of file occurs while no characters have been read.

for e.g

char *ptr = NULL;
while( (ptr = fgets(ar,sizeof(ar), p)) != NULL) {  
   printf("%s",ptr);
   /* fgets() copies \n t the end of buffer, if you don't want remove it by some logic */ 
}

About what fgets() will read, from the manual page

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.

Achal
  • 11,821
  • 2
  • 15
  • 37