ifstream::get() acting strange.

hi:
i defined an input file stream, and its acting fine on some files, but strange on one file.
i'm using this code:
1
2
3
4
5
6
7
...
    ifstream in;
    in.open("something");
    in.get();
    in.seekg(-1,ios::cur);
    in.get();
...

i'm using the debugger that came with the VS2010 to determine the values returned by each function.
this is a normal file, as appeared in my hex editor:


offset(h)  00  01  02  03  04  05  06  07  08  09  0A  0B  0C  0D  0E  0F
00000000   00  00  0D  20  00  00  1B                                        ... ...         


this file is a subset of the original file, means the first few bytes of the original file are the same in this one, but the original has more values.
the program works fine on this short version of the file, but the problem appears when i use the big file:
1
2
3
4
5
6
7
...
    ifstream in;
    in.open("something");
    in.get();  //this returns 0x00.
    in.seekg(-1,ios::cur);  //returns 0x01
    in.get();  //this should return 0x00, instead, it returns 0xffffffff.
...


here's what i found:
it's not an eof problem, i spread several calls to eof() around this code and it returned false.
it's not a file size problem, the original file contains only 1000 values, and in my search i discovered that the file size limit is way over that(unless each value is
2,147,483 bytes long).
i searched if ifstream::get() returns something strange when it finds null, but found nothing.
in a thought, can it be the file system, i'm using NTFS win7 64bit.

i thought to my self (it's time for a professional's advice).
i'd really appreciate the help, as i spent 10 hours searching.
on a second thought, could it be because the file is opened in text mode?
i'll give it a try.
it worked, i just had to open the file in binary mode.
sorry for the waste of time i might have caused.
i just have one more problem now:
i have this code in another part of the program:
1
2
3
4
if ( *(ch+i) == in.peek() )    //ch is char* type, and is initialized.
{
    /* do something */
}

the debugger says something that means:
ch[0] = 0xff 'ΓΏ'
i = 0
peek() returned 0x000000ff

but the condition wasn't met, the /*do something*/ part did not execute.

the return value of peek() and the value of *ch are equal, i don't understand.
well, i think i solved the second issue after an experiment:
i just tried this:
1
2
3
4
if( *(ch+i) == static_cast<char>(in.peek()) )
{
     /*do something here*/
}


it worked like magic.
Last edited on
Topic archived. No new replies allowed.