| | |
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 |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop interest kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming pyramidusingturboccodes radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






