File I/O - rawWrite issues

Idan Arye GenericNPC at gmail.com
Wed May 8 16:17:01 PDT 2013


On Wednesday, 8 May 2013 at 21:34:00 UTC, Carl wrote:
> I am learning D so I thought this would be the correct place 
> for this question (I assume I am making an error, not the 
> compiler).
>
> I am writing arrays of ubytes using rawWrite to a file. 
> Whenever I write arrays full of numbers I have no problems, but 
> once I write an array with zeroes I can then no longer write to 
> that file.
>
> For example:
> ubyte[] data = [5, 34, 9, 45];
> file.rawWrite(data); // OKAY
>
> ubyte[] data = [0, 0, 0, 45];
> file.rawWrite(data); // NOT OKAY
>
>
> I get no errors or exceptions, it just won't write the array. I 
> can't even open a new file stream and write, not even after 
> rerunning the program. My only guess is some kind of string 
> related problem where '\0' causes it to terminate.
>
> Thanks in advance.
>
>
>
> P.S. DMD, Ubuntu 11

Works fine on my Arch linux(64bit):

	import std.stdio;

	void main(){
		auto oFile = File("testfile","w");

		ubyte[] data1 = [0, 0, 0, 45];
		oFile.rawWrite(data1);

		ubyte[] data2 = [1, 2, 3, 4];
		oFile.rawWrite(data2);

		oFile.close();

		auto iFile = File("testfile","r");

		ubyte[16] buffer;

		writeln(iFile.rawRead(buffer));

		iFile.close();
	}

Prints: [0, 0, 0, 45, 1, 2, 3, 4]


If you were working on windows I would have suggested to try 
opening the file in binary mode.


More information about the Digitalmars-d-learn mailing list