Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 KoldGames   Members   -  Reputation: 222

Like
0Likes
Like

Posted 18 September 2013 - 07:14 PM

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, 18 September 2013 - 07:21 PM.


Sponsor:

#2 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 18 September 2013 - 07:27 PM

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.


[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#3 L. Spiro   Crossbones+   -  Reputation: 14262

Like
1Likes
Like

Posted 18 September 2013 - 08:40 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 KoldGames   Members   -  Reputation: 222

Like
0Likes
Like

Posted 18 September 2013 - 10:46 PM

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! :)



#5 L. Spiro   Crossbones+   -  Reputation: 14262

Like
3Likes
Like

Posted 18 September 2013 - 11:26 PM

Because you are reading “sizeof(nameLength)” characters instead of “nameLength” characters.

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#6 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 19 September 2013 - 03:04 PM

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



#7 L. Spiro   Crossbones+   -  Reputation: 14262

Like
0Likes
Like

Posted 19 September 2013 - 03:50 PM

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS