How to reset file being read?

This is a discussion on How to reset file being read? within the C++ Programming forums, part of the General Programming Boards category; Code: if(inFile >> testGrade[0]) found = true; for(int a = 0; a < 4; a++) { inFile >> testGrade[a]; cout ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    18

    How to reset file being read?

    Code:
    if(inFile >> testGrade[0])
    found = true;
    	
    	for(int a = 0; a < 4; a++)
    		{
    		inFile >> testGrade[a];
    		cout << testGrade[a] << " ";
    		}
    	inFile.getline(studentName[0],40);
    	cout << studentName[0];
    		
    }

    Im performing a priming read with this file, but its skipping the first one. How would I go about resetting it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,338
    The problem is that inFile >> testGrade[a] reads in something to testGrade but stops at the whitespace after that value. The whitespace is probably a newline after the last value. Then getline attempts to read a line, but it stops when it finds a newline, so it stops immediately without reading anything in. (The getline function discards the trailing newline but operator>> does not, which is why mixing them can cause these kinds of problems.)

    The solution is to add inFile.ignore() after a call to operator>> to ignore the newline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. c script help to read a file & update
    By indy in forum C Programming
    Replies: 8
    Last Post: 12-01-2003, 10:32 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21