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:

// 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.

while( fgets(s, MAX_BUFF, file) != NULL)
{


}

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.

// 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;
        }
   }

I figured it out..... Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.