February 09, 2005
$ cat writeString.d

import std.stream;

int main()
{
char[] s = "hello\n";

File f = new File("test2", FileMode.OutNew);
f.write(s);
f.close();

return 0;
}


$ dmd  writeString.d
gcc writeString.o -o writeString -lphobos -lpthread -lm
$ ./writeString
$ vi test2
^F^@^@^@hello

The first 4 chars are garbage.

dmd v0.111
gcc version 3.3.3
Linux 2.6.7-gentoo-r11


February 09, 2005
bug@d.com wrote:

> $ cat writeString.d
> 
> import std.stream;
> 
> int main()
> {
> char[] s = "hello\n";
> 
> File f = new File("test2", FileMode.OutNew);
> f.write(s);
> f.close();
> 
> return 0;
> }

Not a bug, just a silly API...

> $ dmd  writeString.d
> gcc writeString.o -o writeString -lphobos -lpthread -lm
> $ ./writeString
> $ vi test2
> ^F^@^@^@hello
> 
> The first 4 chars are garbage.

No, it's not:

# hexdump -C test2 

00000000  00 00 00 06 68 65 6c 6c  6f 0a                    |....hello.|

It's your string structure, with the length first!

If you want to write the string, you instead use:

> f.writeString(s);

http://www.digitalmars.com/d/std_stream.html:

> void write(char[] s)
>     Write a basic type or counted string.

> void writeString(char[] s)
>     Write a string of text

Clear as mud?
--anders