# Reading in more than I want...

This topic is 5194 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

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

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.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 on other sites
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 namefread( &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 on other sites
I thought the NULL-Terminator was written to the file. Obviously, I was wrong! Thank you.

##### 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 on other sites
Ah. That shows what a life time of std::string does to you. [grin]

##### 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 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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 15
• 21
• 26
• 11