Jump to content
  • Advertisement
Sign in to follow this  
spoolin240sx

File Reading/Saving Strings

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

Hey everyone, I am having problems saving and reading strings from a file. It isnt exactly reading it correctly, or I am not saving it correctly I cant seem to get it to work. This is how I am trying to do it: char *FileName; //Save char *EndString; EndString = new CHAR[strlen(this->TileInfo[TileCount].FileName)]; sprintf(EndString, "%s\n", this->TileInfo[TileCount].FileName); fputs(EndString, FileHandle); //Load int Length = fgetc(FileHandle); this->TileInfo[TileCount].FileName = new CHAR[Length]; fgets(this->TileInfo[TileCount].FileName, Length, FileHandle); Does anyone have any ideas?

Share this post


Link to post
Share on other sites
Advertisement
The trouble with your code is, you are trying to load in the size of the string with your call to fgetc but you never save the length. Change your saving code to:

unsigned char Size = strlen((this->TileInfo[TileCount].FileName)+1;
EndString = new char[Size+1];
sprintf(EndString, "%c%s\n", Size,(this->TileInfo[TileCount].FileName);
fputs(EndString, FileHandle);

The +1 in the first line is for the character we append to the start with the length of the string. The +1 in the second line is for the null terminated end of the string.

As we load the string back in with the size for its length we will already have the +1 for the null termination in the loading code so don't need one there).

As you can see, you now append a character holding the (length+null termination) of the original string to the start of the string you save.

This will put a limit of 255 characters on your string though.

Share this post


Link to post
Share on other sites
Drazgal, i forgot to post my code i actually was saving the length sorry. its still not reading it right it reads like maybe 4 or 5 characters then it says the filename its weird. i am using fputc before the fputs for other information could that be the culprit?

Share this post


Link to post
Share on other sites
the second string starts off with a bunch of spaces before it. any ideas?
the first one works, second one starts off with spaces, and the third one works fine also. here is the code guys:

//saving:
char *EndString;
int Size = 0;

Size = strlen(this->TileInfo[TileCount].FileName)+1;
fputc(Size, FileHandle);
EndString = new char[Size];
sprintf(EndString, "%s\n", this->TileInfo[TileCount].FileName);
fputs(EndString, FileHandle);

//loading:
int Length = 0;

Length = fgetc(FileHandle);
this->TileInfo[TileCount].FileName = new CHAR[Length];
fgets(this->TileInfo[TileCount].FileName, Length, FileHandle);

any ideas? the second one is fine i saved it a few times and retried but same thing, spaces before the name. any reason for it?

[Edited by - spoolin240sx on April 24, 2005 11:05:24 PM]

Share this post


Link to post
Share on other sites
Ok, if i do:
Size = strlen(this->TileInfo[TileCount].FileName)+2;
it gets the right names, but i think theres an extra character in that string that i cant see and i cant pass that to another function to load by those filenames, how can i take the last character off a string?

edit: +2 not +1

[Edited by - spoolin240sx on April 25, 2005 12:42:49 AM]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!