Jump to content
  • Advertisement
Sign in to follow this  
spoolin240sx

Removing Null Character

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

I am loading file names from my own file type, and it fails because the string has a null character appended to it. How can I remove the null character? edit: the string is two characters longer than it should be. any way to remove the last two characters from a string?

Share this post


Link to post
Share on other sites
Advertisement
You don't want to get rid of the null character, because that's what marks the end of the string. Why, exactly, is this character causing the operation to fail?

CM

Share this post


Link to post
Share on other sites
If it's always 2 characters long you could use something like
//cin.get( char* s, streamsize n );
cin.get(buffer, strlen(fileName)-3);

Share this post


Link to post
Share on other sites
Quote:
Original post by Sfpiano
If it's always 2 characters long you could use something like
//cin.get( char* s, streamsize n );
cin.get(buffer, strlen(fileName)-3);


First of all you should never use (char *) when using iostream functions, you should only use std::string types. and if needed later you can convert them via string.c_str() to a (char *). and a Null Terminated is only 1 char at the end of a string and is uaually '\0' but on most std::string function this char is excluded and doesn't need to be worried about.


---

Any how.. spoolin240sx, you need to post some code so we can tell if what your doing and how your handeling the data.. otherwise we can't tell how to fix it.

Share this post


Link to post
Share on other sites
thanks for the replys, here is some code:

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

i know the null character is causing the function to fail after this because if i hardcode those filenames in the program with null character at the end it fails. and when i load these file names, i message box them so i can see what they are, and they are correct, but the unseen null character is messing my D3DXCreateTextureFromFileEx function.

Share this post


Link to post
Share on other sites
Quote:
Original post by spoolin240sx
thanks for the replys, here is some code:

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

i know the null character is causing the function to fail after this because if i hardcode those filenames in the program with null character at the end it fails. and when i load these file names, i message box them so i can see what they are, and they are correct, but the unseen null character is messing my D3DXCreateTextureFromFileEx function.

I don't think you fully understand strings. You *have* to have a null terminator, or every function that reads your string will fail. This includes D3DXCreateTextureFromFileEx, and basically ever other string manipulation function you can think of.

fgets(str, len, file) will read up to len-1 characters, or up to the first newline/EOF it encounters, whichever comes first. Then it will append a null terminator to the string. You've only created enough space for length-2 characters. So you're going to come up two characters short. This is an issue.

Without more information, that's the best I've got for you.

CM

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!