Sign in to follow this  

Read string of unknown size from binary using ifstream - C++

This topic is 1580 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

Hello! I have my own custom binary model format and I am trying to read it using ifstream, but ran into a problem. I have a class called NXNode which contains the name of each scene node. The problem is that the name's size varies. How would I read the string correctly when I don't know it's size? I know BinaryReader.ReadString() does it without me specifying the size, so it must be possible with C++ too right? Thanks! smile.png

 

[EDIT: Here's some code if it helps]

NXNode::NXNode(ifstream* reader, NXNode* parent) : NXNode()
{
	if (parent != NULL)
		Parent = parent;
	
	reader->read((char*) &Name, sizeof(string));
	reader->read((char*)&HasChildren, sizeof(bool));
	reader->read((char*)&MeshCount, sizeof(short));
	reader->read((char*)&ChildCount, sizeof(short));
	
	float nums[16];
	for (int i = 0; i < 16; i++)
	{
		reader->read((char*) &nums[i], sizeof(float));
	}

	if (HasChildren)
	{
		Children = new NXNode[ChildCount];
		for (int i = 0; i < ChildCount; i++)
		{
			Children[i] = NXNode(reader, this);
		}
	}
}
Edited by KoldGames

Share this post


Link to post
Share on other sites

What is usually done is either a few bytes are prefixed to the string that tell you how many characters are in it, or the string is NULL terminated (so you write the trailing '\0' when you write the string) and when you read you just keep reading until you hit the NULL character.

Share this post


Link to post
Share on other sites

Just to throw more onto what was already said, it is typically preferred to store the length of the string and then read the whole string all at once instead of repeatedly reading a single character at a time for performance reasons.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

Hello! So I now the length for each string in my file. In this case, the length is 7 (box.obj). Here's how it's setup now:

int nameLength;
reader->read((char*) &nameLength, sizeof(int));
reader->read((char*) &Name, sizeof(nameLength));

But now, it won't read. My app crashes. Am I doing that the write way? Thanks! :)

Share this post


Link to post
Share on other sites

Note that depending on the type of Name, reading and writing using &Name could be very bad ju-ju.

Share this post


Link to post
Share on other sites

And while we are on that note of precautions-

 

If “Name” is some kind of pointer then it should be allocated to be able to hold nameLength characters (or (nameLength + 1) if the ternimator is not included).

If “nameLength” does not include the terminating '\0' character, it must be explicitly added since it is not being added during the read.

 

 

L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this