[prog] stream eof problem

Elizabeth Barham lizzy at soggytrousers.net
Sun Mar 9 15:25:38 EST 2003


Jennifer Davis <davi0302 at algonquinc.on.ca> writes:

> 				inFile.clear();
> 				do
> 					clearChar=inFile.get();
> 				while (clearChar!='\n');

   Infinite loop. By changing the guard to include !inFile.eof() (as
in 'while(clearChar != '\n' && !inFile.eof())' the guard is guaranteed
to eventually become false.

  // immediatly after reading the 24th item:

    while (!inFile.eof()){

      // positioned *after* the last newline but before a read
      // that will tell it if its eof

      // probably fails:

      inFile >> product[filesInputted].item_code;

      // fails condition; go to bottom:

      if (!inFile.fail()){ // outer
	inFile >> product[filesInputted].quantity_on_hand;
	if (!inFile.fail()){ // middle
	  inFile >> product[filesInputted].unit_selling_price;
	  if (!inFile.fail()){ // inner
	    filesInputted++;
	    cout << filesInputted << " ";
	  } //endif - inner
	}// endif - middle
      } else { //endif - outer
        // ** HERE **
        // ** inFile probably has eof set but since its cleared **
        // ** there is no way to determine it before **
	inFile.clear();
	do
	  clearChar=inFile.get();
	while (clearChar != '\n' && ! inFile.eof());
      } // end if - else
      cout << endl;
    }// end while

   If you are interested, I watched the program run via gdb after
applying the -g switch with g++;

  $ g++ -g -o jenns jenns.cpp
  $ gdb jenns
 
Elizabeth


More information about the Programming mailing list