r/w binary

Joel Christensen joelcnz at gmail.com
Wed Jun 29 00:17:09 PDT 2011


Thanks for your reply Ali.

Your right about changing create to open when reading. And, yes, I was 
thinking of trying std.stdio myself.

- Joel

On 29-Jun-11 6:48 PM, Ali Çehreli wrote:
> 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