Sign in to follow this  
caldiar

ifstream.get - trailing char?

Recommended Posts

This is nothing to get hung about but it just has me curious. I'm reading a binary file and have a unsigned char array that is 4 elements in size. I have an ifstream called input and use input.get(array, 4) to grab the first four bytes of the input file and dump it in to the array. However, when I print out the contents of the array I only get the first 3 elements + some weird looking symbol. grabbing 5 elements (input.get(array, 5)) ends up printing the correct value though. I'm wondering what the deal with this is. Is it normal for ifstream.get() to slap an extra byte on or should I take a closer look at my code because I made it mad somehow? Code:
bspHeader_t *header = new bspHeader_t; //contains unsigned char type[4];

	ifstream input("mapname.bsp", ios::binary);
	input >> hex;
        input.get((char*)header->type, 5);
	
	cout << header->type << endl;

Thanks, Caldiar [Edited by - caldiar on January 18, 2009 4:13:50 AM]

Share this post


Link to post
Share on other sites
Which OS are you using? There was a thread yesterday that linked to an older thread where someone just getting into c++ and discovered the same thing: In his case, Windows worked just fine, but Linux was throwing in an extra character... I don't know if that will help, but you might search for the thread. The link can't be more than a day or two old.

Share this post


Link to post
Share on other sites
Quote:
The fourth function extracts up to _Count - 1 elements and stores them in the array beginning at _Str. It always stores char_type after any extracted elements it stores. In order of testing, extraction stops:

* At end of file.
* After the function extracts an element that compares equal to _Delim, in which case the element is put back to the controlled sequence.
* After the function extracts _Count - 1 elements.


From MSDN. I'd say that you're seeing expected behavior. The size you pass in is basically the maximum number of elements in your array, and get() will extract elements until it has only one slot left, where it will put a NULL.

Share this post


Link to post
Share on other sites
Thanks for the reply guys.

It isn't an issue when dealing with char arrays but when I do something like this:

int value;
ifstream input(binaryfile, ios::binary);
input.get(&value, 4);



Value will be a different number than it should be but if I change input.get(&value, 4) to input.get(&value, 5), value becomes the correct number.

The only problem though is during runtime I get a pop-up error saying that the stack around variable is corrupted. This is a debug build and I haven't tried a release build yet though. This is on a Windows XP SP 2 machine and compiled with whatever compiler comes shipped with Visual Studio 2005 Standard Edition.

If I ignore the corrupt stack error the program continues to run successfully but an error's an error and should be corrected.

Is there a way to force ifstream's get() function to not slap a null on to the end of the data extracted?

Share this post


Link to post
Share on other sites
haha it is, indeed, the function I needed. I'm not sure how I overlooked it (I had been using that in a different function and completely forgot it existed)

Thanks a ton. It's reading the data just fine now. And no corruption in the stack :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this