www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Weird stream write output

reply "alkololl" <meltor trash-mail.com> writes:
Hi,

My program does not do what I want it to do.

import std.stdio;
import std.stream;

int main(string[] argv) {
	Stream file = new BufferedFile("D:\\test.txt", FileMode.OutNew);
	char[] patch = [ 0x41, 0x41, 0x41, 0x41, 0x41 ];

	file.write(patch);
	file.close();

	writeln("press enter to exit");
	getchar();
	return 0;
}

gives me this output (the following line is the content of 
test.txt)
|   AAAAA

As you can see there are another 5 characters right before the 
five 'A'.
It seems that D fails with the proper encoding?

Thansk for help
Nov 27 2013
parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
file.write writes the length of the array, which is what you see:

     Writes a string, together with its length.
     The format is implementation-specific and should only be used 
in conjunction with read. Throw WriteException on error.

fix: file.writeString(patch);

(BTW, notice that use of std.stream is not recommended)
Nov 27 2013
parent reply "alkololl" <meltor trash-mail.com> writes:
On Thursday, 28 November 2013 at 00:05:24 UTC, Luís Marques wrote:
 file.write writes the length of the array, which is what you 
 see:

     Writes a string, together with its length.
     The format is implementation-specific and should only be 
 used in conjunction with read. Throw WriteException on error.

 fix: file.writeString(patch);

 (BTW, notice that use of std.stream is not recommended)
Hi thanks for your answer. I need to use streams because i need to set the file pointer in order to apply a binary edit within an executable. What would you recommend me, knowing this? thank you
Nov 27 2013
parent reply =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
On Thursday, 28 November 2013 at 00:15:48 UTC, alkololl wrote:
 What would you recommend me, knowing this?
Maybe use the core.stdc.stdio (.seek, etc.) ?
Nov 27 2013
parent reply "alkololl" <meltor trash-mail.com> writes:
On Thursday, 28 November 2013 at 00:29:26 UTC, Luís Marques wrote:
 On Thursday, 28 November 2013 at 00:15:48 UTC, alkololl wrote:
 What would you recommend me, knowing this?
Maybe use the core.stdc.stdio (.seek, etc.) ?
Okay, thank you so much. Is there any good lecture you can recommend me reading? Unfortunately, I dont think I have understood 10% of D
Nov 27 2013
parent =?UTF-8?B?Ikx1w61z?= Marques" <luis luismarques.eu> writes:
On Thursday, 28 November 2013 at 00:39:39 UTC, alkololl wrote:
 Is there any good lecture you can recommend me reading?
Ali Çehreli's book is online, in HTML (http://ddili.org/ders/d.en/index.html) and PDF (http://ddili.org/ders/d.en/Programming_in_D.pdf). You can also look for Andrei Alexandrescu's book, The D Programming Language, which is quite a nice reading, although *slightly* out of date. For many years I was not aware of D's IRC channel in EFnet, #d. Go there, the people are really nice, and it's useful for quick questions. You might also want to post in D.learn.
Nov 27 2013