Jump to content
  • Advertisement
Sign in to follow this  

ifstream problem - weird characters

This topic is 2123 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! So I created a writer a while back using C# that creates a custom format for my game engine. I'm trying to create a reader in c++ but I am having problems. So I have a struct called NXShaderInfoBlock. It contains 5 char* arrays:

char* Name;
char* VSEntry;
char* PSEntry;
char* VSProfile;
char* PSProfile;

It's constructor takes an ifstream and uses it to read the block. I first read the lengths of my char arrays into shorts, then I go into a for loop for each length, and load data into the char* array.

short* lengths = new short[5];

for (int i = 0; i < 5; i++)
	reader->read((char*)&lengths[i], sizeof(short));

Name = new char[lengths[0]];
VSEntry = new char[lengths[1]];
PSEntry = new char[lengths[2]];
VSProfile = new char[lengths[3]];
PSProfile = new char[lengths[4]];

for (int i = 0; i < lengths[0]; i++)
	reader->read(&Name[i], sizeof(char));

for (int i = 0; i < lengths[1]; i++)
	reader->read(&VSEntry[i], sizeof(char));

for (int i = 0; i < lengths[2]; i++)
	reader->read(&PSEntry[i], sizeof(char));

for (int i = 0; i < lengths[3]; i++)
	reader->read(&VSProfile[i], sizeof(char));

for (int i = 0; i < lengths[4]; i++)
	reader->read(&PSProfile[i], sizeof(char));

Now I've inserted a breakpoint at the end of the method, and it seems to be reading the lengths right, but I get these weird characters at the end of the correct data:




As you can see, it gets the data I want (Color, VS, PS, vs_4_0, ps_4_0), but then adds all those weird characters after it. Any help? Thanks! :)


Share this post

Link to post
Share on other sites

Let me guess. In the string you've written, you aren't accounting for the null teriminating character, are you? C-style strings are null terminated and need a '\0' at the end of the string. Otherwise, if you try to print the string (or use it some other way), the code will try to read the string and keep reading off into random memory until it hits a '\0'. Without the '\0', it has no idea where the end of the string actually is.


You either need to write a '\0' in your output stream (so you can read it back later), or when you read, you need to allocate 1 extra character for each string and set the last character to '\0'.


Also, I'd suggest not using raw pointers like that. You can have each member be a std::string, and resize the string to the right size, and then read data into the string. Also, std::vector is your friend (so you don't have to say new short[]). Note that if you use std::string, you don't need to worry about the null terminating character. And one last thing: when reading your string, I would suggest reading the string all in one go, not one character at a time. You already know its size and have allocated the memory for it.

Edited by Cornstalks

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!