Welcome to Emulationworld

Forum Index | FAQ | New User | Login | Search

Make a New PostPrevious ThreadView All ThreadsNext Thread**


SubjectC++ weirdness Reply to this message
Posted bySnowball 2
Posted on07/08/04 05:01 PM



Here's the deal. I'm reading from an input file with an ifstream object. I use the read() function to grab either a byte or a word. When I grab bytes there's no problems using seekg() and tellg(). Here's essentially what I'm doing and what I want to do:

 
//Save the position of the file pointer
int position = input.tellg();
//The buffer that reads in bytes or words
char *inputbuffer = new char[charsize];

while(some condition)
{
input.read(inputbuffer, charsize);
}

if(should I go back?)
//Restore the pointer
input.seekg(position);

I'm reading in 2 separate formats of text for the input file. If it's ANSI text there's a character at every byte and seekg restores me to where I should be. If it's UCS-2 I read in a word and the character is either at the lower order or higher order byte(higher in this specific case). When I restore my pointer it will point to the byte that is between the characters in UCS-2 which contains only the value 0. At least that's the way it happens when I try to read it afterwards.
input.read(inputbuffer, charsize); 
//inputbuffer[0] = 0x00 and all successive calls to read will grab zeros


After that it breaks my program as it's pulling input that once looked like CHARACTER-ZERO that's now ZERO-CHARACTER and I can't tell if I skipped the previous character by accident or that I'm reading half of the next.

I need to understand where exactly the pointer is dropping me off. I'm using raw read and write functions of ifstream because using the character grabbing functions won't be as reliable especially with UCS-2 encoding(and other formats may be included as well so just widening the characters won't be enough).

Anyone want to shed some light on this? I can include more code and sample input later if necessary.