Sign in to follow this  

Reading a variable length string from stream

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to read a variable length string, with a length given earlier in the file, from a binary file. This is a pretty standard operation for binary file formats I believe but I just can't get it working. My c++ code so far:
// filenameLength is defined somewhere before
char* filename = new char[filenameLength];
file.read(filename, filenameLength);

std::cout << "Filename: " << filename << std::endl;
Now this compiles okay, but my results aren't exactly accurate. If the filename given in the binary file was "example.file", I get results like "example.file²²²²||". The value of filenameLength is always correct (12 in that case.) Is this the right way to be doing something like this?

Share this post


Link to post
Share on other sites
You have two problems. First, you aren't allocating room for the terminating null in your character array which means further operations on the array will be unable to determine the endpoint of the string and will run into uninitialized memory and BLAH BLAH BLAH. Translation: Use std::string instead of char arrays.

Your second problem: the extraction operator (by default) will read up until the next whitespace or EOF. If you specifically want to read exactly 12 bytes, you can use istream::read(). That's only half your problem, though. Quit using arrays for stuff like this... they're ugly and outdated.

Share this post


Link to post
Share on other sites
Just to make explicit what Sneftel was talking about, the reason you're getting junk shown after your filename when you print is because of you're not null terminating the filename. You should use:


char* filename = new char[filenameLength+1]; // add room for the '\0';
file.read(filename, filenameLength);
filename[filenameLength] = '\0'; // null terminate the string


Although, at the risk of a good flaming, I disagree that using arrays for this instance is a bad thing. Personally, I dislike using streams for binary files, prefering to use fixed length arrays and the "old fashioned" fwrite/fread. Again, though, this is only for binary files.

Share this post


Link to post
Share on other sites
There's no reason at all not to use std::string here too. Mixing the two is fine--for instance, if you're dealing with immutable strings, passing around pointers can sometimes be slightly faster--but when you're actually allocating and filling space like this, there's just no good reason to, and plenty of reasons not to (looks like you just discovered one).

Share this post


Link to post
Share on other sites

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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