| | |
Checking EOF while using fgets()... Confused.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 9
Reputation:
Solved Threads: 0
Hello again... So, I'm trying to read a file line-by-line and compare the first 5 characters of that line (that was read in) to another character string. If they don't match then it moves on to the next line of the file and tries to compare again. The problem I'm encountering is that when it reaches the End of File it isn't stopping the loop. It works well until it gets to the last line of the file, then it just gets into an infinite loop with that string in the buffer. Here's my code:
Where argv[1] is the argument, "datafile" which contains the following:
IPHON 400.00 500.00
WALMA 50.00 75.00
KROGE 100.00 120.00
CHOOC 24.00 34.24
MICRO 1000.00 2000.00
My code works relatively well until it gets to the final line: MICRO 1000.00 2000.00, then it just gets in a loop. Here's a quick example when I use CHOOC as the stock_id that is being compared.
-bash-3.2$ ./StockOrders datafile logfile server client
Broker is waiting for request...
Request quote to buy CHOOC by 5555.
Client says, "Request quote to buy CHOOC by 5555.
."
Client wants to buy...
Printing stock_id: CHOOC
Printing cust_id: 5555
Broker is generating quote...
Printing s: IPHON 400.00 500.00
Cannot find the stock in datafile...
Printing s: WALMA 50.00 75.00
Cannot find the stock in datafile...
Printing s: KROGE 100.00 120.00
Cannot find the stock in datafile...
Printing s: CHOOC 24.00 34.24
Found the stock in datafile!
-bash-3.2$
Thanks in advance!
C Syntax (Toggle Plain Text)
// Read into DATAFILE and get Stock Info file = fopen(argv[1], "r"); // fgets(s, MAX_BUFF, file); // printf("Printing s from datafile: %s\n", s); while(1) { fgets(s, MAX_BUFF, file); printf("Printing s: %s\n", s); if(ferror(file)) { break; } if(s == NULL) { break; } if(strncmp(s, stock_id, 5) == 0) { printf("Found the stock in %s!\n", argv[1]); break; } else printf("Cannot find the stock in %s...\n", argv[1]); }
Where argv[1] is the argument, "datafile" which contains the following:
IPHON 400.00 500.00
WALMA 50.00 75.00
KROGE 100.00 120.00
CHOOC 24.00 34.24
MICRO 1000.00 2000.00
My code works relatively well until it gets to the final line: MICRO 1000.00 2000.00, then it just gets in a loop. Here's a quick example when I use CHOOC as the stock_id that is being compared.
-bash-3.2$ ./StockOrders datafile logfile server client
Broker is waiting for request...
Request quote to buy CHOOC by 5555.
Client says, "Request quote to buy CHOOC by 5555.
."
Client wants to buy...
Printing stock_id: CHOOC
Printing cust_id: 5555
Broker is generating quote...
Printing s: IPHON 400.00 500.00
Cannot find the stock in datafile...
Printing s: WALMA 50.00 75.00
Cannot find the stock in datafile...
Printing s: KROGE 100.00 120.00
Cannot find the stock in datafile...
Printing s: CHOOC 24.00 34.24
Found the stock in datafile!
-bash-3.2$
Thanks in advance!
loop is incorrect. Here is how to read until end of file. fgets() returns NULL on end-of-file or some other type of error.
C Syntax (Toggle Plain Text)
while( fgets(s, MAX_BUFF, file) != NULL) { }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Nov 2008
Posts: 9
Reputation:
Solved Threads: 0
Ahh, I see... Thank you. Well, that small snippet allowed it to stop when needed but now for some reason it's not comparing the last line with the stock_id (MICRO - everything else works perfectly). I don't see anything wrong with that piece of code either.
C Syntax (Toggle Plain Text)
// Read into DATAFILE and get Stock Info file = fopen(argv[1], "r"); // fgets(s, MAX_BUFF, file); // printf("Printing s from datafile: %s\n", s); while(fgets(s, MAX_BUFF, file) != NULL) { printf("Printing s: %s\n", s); if(ferror(file)) { break; } if(strncmp(s, stock_id, 5) == 0) { printf("Found the stock in %s!\n", argv[1]); break; } }
![]() |
Other Threads in the C Forum
- Previous Thread: structures giving problem
- Next Thread: can any body explain this program
| Thread Tools | Search this Thread |
* ansi api append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string strings suggestions test testautomation threads unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






