ifstream.get() returns wrong character

Feb 14, 2014 at 7:12pm
Hello,

I have a really confusing problem. Was searching the web, but found nothing useful.

It is simple: I want to remember the position in a file by calling tellg(), read two characters and return to the position before I read by calling seekg(). This seem to work, but when I proceed reading from the file, the next read character is the one just if the seekg() has never been called. Please take a look at the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool D3DXRipDrawCallSplitter::ThereIsVertex()
{
	int filePos;
	int i1, i2;

	filePos = objBr.tellg();	// returns 35
	i1 = objBr.get();		// read two characters
	i2 = objBr.get();

	objBr.seekg(filePos);		// set the file pointer back to 35
	filePos = objBr.tellg();	// returns 35. Seems to be allright
	i1 = objBr.get();	        // Fail! reads value at position 37
	if (!(i1 == 118 && i2 == 32))
	{
		return false;
	}

	return true;
}


the call of seekg() in line 10 sets the file pointer to the right position (35). but the next call of get() in line 12 returns the character at position 37. can you explain this???
Feb 16, 2014 at 10:13am
Nobody who has an idea???
Feb 16, 2014 at 10:29am
try using gets() instead of get().
Topic archived. No new replies allowed.