943,769 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 14130
  • C RSS
Nov 24th, 2008
0

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

Expand Post »
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

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

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. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 24th, 2008
0

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

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008
Nov 24th, 2008
0

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

I figured it out..... Thanks!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Krysis is offline Offline
15 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: structures giving problem
Next Thread in C Forum Timeline: can any body explain this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC