Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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: 6995

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: 17337

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



#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: 17337

Like
3Likes
Like

Posted 18 September 2013 - 11:26 PM

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

 

 

L. Spiro



#6 SiCrane   Moderators   -  Reputation: 9866

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: 17337

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






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