0

My problem statement is in such a way that I run a command and save the output of the command in a buffer. My while loop looks for a particular string in the output and if the string exists, runs the required function.

To end the while loop I have mentioned feof, in the while loop but the loop doesn't work!

Here is the snap of the code, I want to end the while loop in an optimized way!

FILE *fp = popen("ls","r");
char store[128];

while((fgets(store,sizeof store, fp)!=NULL) || (feof(fp))){
    if(strcasestr(buffer,"abc")){
        printf("\nSuccess");
        some_other_function();
    }
}
pclose(fp);
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
P.W
  • 63
  • 5
  • Logically, you would put `while (!feof(fp))`. However, that is also wrong: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – lost_in_the_source Sep 12 '19 at 00:32
  • 2
    That code has logic that might be described as "keep looping so long as we successfully read a line or we're at the end of the file". – Ken Thomases Sep 12 '19 at 00:32
  • 2
    You don't need the `feof()` check. `fgets()` will return `NULL` when you get to EOF. – Barmar Sep 12 '19 at 00:32
  • @Barmar: More precisely, when it reaches the end of the file. `EOF` is a macro that expands to a negative integer (typically `(-1)`) which is the value returned by certain functions on encountering an end-of-file or error condition. – Keith Thompson Sep 12 '19 at 01:07
  • @KeithThompson I was using EOF as the generic abbreviation for End-Of-File. – Barmar Sep 12 '19 at 01:09
  • @Barmar: Yes, I know. I suggest not doing that, because `EOF` has a very precise meaning in C. – Keith Thompson Sep 12 '19 at 02:23
  • @KeithThompson Sorry, it's a very old habit, hard to break. I don't see how "get to EOF" could be confused as comparing something with EOF, especially since I just said the value is NULL. – Barmar Sep 12 '19 at 03:39

2 Answers2

1

The loop condition should be:

while( fgets(store,sizeof store, fp) != NULL )

There is no reason to also check feof at this point. If end of file was reached but some characters were read then you do want to process the string anyway.

You could check feof after the loop to determine whether input failed due to a read error, or due to the end of the file.

M.M
  • 138,810
  • 21
  • 208
  • 365
-2

Replaced the while loop with this, and now it works! Thanks !

while((fgets(store,sizeof store, fp)!=NULL) && !(feof(fp)))
P.W
  • 63
  • 5
  • ugh - again please see Edward's comments above. – Michael Dorgan Sep 12 '19 at 00:45
  • You still don't need to test `feof(fp)`. `fgets()` performs that test itself. – Barmar Sep 12 '19 at 01:10
  • @MichaelDorgan Edward's comment is only relevant to writing `while (!feof(fp))`. But if you put the `fgets()` call first, it mitigates that problem. – Barmar Sep 12 '19 at 01:11
  • @Barmar: And `feof(fp)` will never be true if there's an input error. (After `fgets()` reports that it's run out of input, you can *then* call `feof(fp)` and/or `ferror(fp)` to learn more about why it ran out of input.) – Keith Thompson Sep 12 '19 at 04:05