www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - bug: write string

reply bug d.com writes:
$ 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
Feb 09 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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: 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
Feb 09 2005