r/w binary

Joel Christensen joelcnz at gmail.com
Wed Jun 29 20:52:59 PDT 2011


I'm thinking more about handling binary files. With the C version I 
would write a int for how many letters in the string, then put in the 
the string along side ([0005][house]). That way I can have any character 
at all (though I just thinking of char's).

Actually, I've just looked the output file and it's a text file. So in 
that case I would use read line to have spaces in strings, though what 
if I wanted to have new line character(s) in the one string. I still 
want to work with binary files.

On 30-Jun-11 2:23 AM, Ali Çehreli wrote:
> On Wed, 29 Jun 2011 19:55:38 +1200, Joel Christensen wrote:
>
>> With the char[], I can't use spaces in it the way I've got it here,
>> (like if I tried using a phrase):
>
> There has been a thread very recently about reading strings. Look for the
> thread "readf with strings" (dated 22-Jun-2011 in my reader). Or, if it
> works here:
>
> http://www.digitalmars.com/webnews/newsgroups.php?
> art_group=digitalmars.D.learn&article_id=27762
>
> Reading the entire line:
>
>      string s = chomp(readln());
>
> Kai Meyer suggested parsing the string directly:
>
>       string[] buffer;
>       int a;
>       float b;
>       string c;
>       buffer = chomp(readln()).split(" ");
>       a = to!(int)(buffer[0]);
>       b = to!(float)(buffer[1]);
>       c = buffer[2..$].join(" ");
>       writef("Read in: '%d' '%f' '%s'\n", a, b, c);
>
>>
>> void saveLevel( string fileName ) {
>> 	int ver = 1;
>> 	auto house = "two".dup;
>> 	double rnum = 3.0;
>> 	
>> 	{
>> 		auto fout = File( fileName, "wb"); // open for binary
> writing scope(
>> 		exit )
>> 			fout.close;
>
> You are not supposed to need to close the File object yourself. Being a
> struct, its destructor should be called automatically.
>
> Ali



More information about the Digitalmars-d-learn mailing list