r/w binary

Ali Çehreli acehreli at yahoo.com
Tue Jun 28 23:48:54 PDT 2011


I've settled on std.stdio as opposed to std.stream and std.cstream.

On Wed, 29 Jun 2011 10:07:13 +1200, Joel Christensen wrote:

> I want to save and load levels for my game. The std.stream module
> doesn't have much examples.
> 
> Here is my code:
> void saveLevel( string fileName ) {
> 	auto bfile = new std.stream.File;
> 
> 	int ver = 1;
> 	string verStr = "version:";
> 	with( bfile ) {
> 		scope( exit )
> 			close;
> 		create( fileName );
> 		write( verStr ); write( ver ); // version
> 	}
> 
> 	int ver2;
> 	char[] verStr2;
> 	auto bfile2 = new std.stream.File;
> 	with( bfile2 ) {
> 		scope( exit )
> 			close;
> 		create( fileName );

That is copy-paste mistake, right? You don't want create() before 
reading. You must have meant open:

		open( fileName );

It works with that change.

> 		read( verStr2 ); read( ver2 ); // version
> 	}
> 	writeln( verStr, ver );
> }
> 
> And this is the result:
> std.stream.ReadException at std\stream.d(46): Stream is not readable
> 
> - Joel

Ali


More information about the Digitalmars-d-learn mailing list