Jump to content
  • Advertisement
Sign in to follow this  
rpg_code_master

Reading in more than I want...

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

This is a bit of a weird bug I think. Simply enough, I’m just writing a string to a binary file, then reading it back. The problem though, is that either the string is getting written with 'garbage', or it is being read with 'garbage'. Here is the writing and reading code:- Writing:-

char* strsss = "Blah";
n = strlen( strsss );//m_Dir.GetCurrentDir( )->GetName( ).length( );
fwrite( &n, sizeof( int ), 1, m_fp );
//	fprintf( m_fp, "%s", strsss/*m_Dir.GetCurrentDir( )->GetName( ).c_str( )*/ );
fwrite( strsss/*m_Dir.GetCurrentDir( )->GetName( ).c_str( )*/, sizeof( char ), n, m_fp );


As you can see, I have tried a few different approaches. Reading:-

// read in the directory name
fread( &n, sizeof( int ), 1, m_fp );
strName = new char[ n ];
memset( strName, '\0', sizeof( char ) * n );
fread( strName, sizeof( char ), n, m_fp );
m_Dir.GetCurrentDir( )->AddDir( strName );
m_Dir.SetDir( strName );
delete [ ] strName;
strName = NULL;


When strName is read in, it should, according to the writing code, be equal to "blah". But it is in fact equal to "blah>>>====" (the garbage >= 129)

Share this post


Link to post
Share on other sites
Advertisement
You forgot about the fact that C strings are 0-terminated. You need to allocate one extra character and set that to 0 after reading in the first part of the string.

Try this:
// read in the directory name
fread( &n, sizeof( int ), 1, m_fp );
strName = new char[ n + 1 ];
fread( strName, sizeof( char ), n, m_fp );
strName[n] = '\0';
m_Dir.GetCurrentDir( )->AddDir( strName );
m_Dir.SetDir( strName );
delete [ ] strName;
strName = NULL;

Share this post


Link to post
Share on other sites
It isn't written to file because strlen doesn't count it. Another way to solve the same problem would be to change the writing code to:

char* strsss = "Blah";
n = strlen( strsss ) + 1;//m_Dir.GetCurrentDir( )->GetName( ).length( );
fwrite( &n, sizeof( int ), 1, m_fp );
// fprintf( m_fp, "%s", strsss/*m_Dir.GetCurrentDir( )->GetName( ).c_str( )*/ );
fwrite( strsss/*m_Dir.GetCurrentDir( )->GetName( ).c_str( )*/, sizeof( char ), n, m_fp );


(note the extra + 1)
and use the original reading code. However, this will take 1 extra byte per string extra and is thus a less optimal solution [wink].

Share this post


Link to post
Share on other sites
Btw, instead of the C fread family of functions, why aren't you using fstreams and their .read()/.write() member functions? :)

Share this post


Link to post
Share on other sites
I've never really used them. I just know the C I/O methods a lot better than I do the STL ones. I find that sometimes, it is easier just to use the C functions. STL over complicates things sometimes IMO.

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!