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: Krysis is an unknown quantity at this point 
Solved Threads: 0
Krysis Krysis is offline Offline
Newbie Poster

Checking EOF while using fgets()... Confused.

 
0
  #1
Nov 24th, 2008
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:

  1. // Read into DATAFILE and get Stock Info
  2. file = fopen(argv[1], "r");
  3. // fgets(s, MAX_BUFF, file);
  4. // printf("Printing s from datafile: %s\n", s);
  5. while(1)
  6. {
  7. fgets(s, MAX_BUFF, file);
  8. printf("Printing s: %s\n", s);
  9.  
  10. if(ferror(file))
  11. {
  12. break;
  13. }
  14. if(s == NULL)
  15. {
  16. break;
  17. }
  18. if(strncmp(s, stock_id, 5) == 0)
  19. {
  20. printf("Found the stock in %s!\n", argv[1]);
  21. break;
  22. }
  23. else
  24. printf("Cannot find the stock in %s...\n", argv[1]);
  25. }

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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,440
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Checking EOF while using fgets()... Confused.

 
0
  #2
Nov 24th, 2008
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.
  1. while( fgets(s, MAX_BUFF, file) != NULL)
  2. {
  3.  
  4.  
  5. }
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: Krysis is an unknown quantity at this point 
Solved Threads: 0
Krysis Krysis is offline Offline
Newbie Poster

Re: Checking EOF while using fgets()... Confused.

 
0
  #3
Nov 24th, 2008
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.

  1. // Read into DATAFILE and get Stock Info
  2. file = fopen(argv[1], "r");
  3. // fgets(s, MAX_BUFF, file);
  4. // printf("Printing s from datafile: %s\n", s);
  5. while(fgets(s, MAX_BUFF, file) != NULL)
  6. {
  7. printf("Printing s: %s\n", s);
  8.  
  9. if(ferror(file))
  10. {
  11. break;
  12. }
  13. if(strncmp(s, stock_id, 5) == 0)
  14. {
  15. printf("Found the stock in %s!\n", argv[1]);
  16. break;
  17. }
  18. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: Krysis is an unknown quantity at this point 
Solved Threads: 0
Krysis Krysis is offline Offline
Newbie Poster

Re: Checking EOF while using fgets()... Confused.

 
0
  #4
Nov 24th, 2008
I figured it out..... Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC