Appending char[] to char[][] has unexpected results

anonymous anonymous at example.com
Tue Apr 30 21:33:27 PDT 2013


On Wednesday, 1 May 2013 at 03:54:23 UTC, Tim Keating wrote:
> Not sure whether this is a bug, or perhaps I'm misunderstanding 
> something, but it seems like this should work:
>
> void main()
> {
> 	char[][] outBuf;
> 	auto f = File("testData.txt", "r");
> 	char[] buf;
>
> 	writeln("\n**** RAW OUTPUT *****");
>
> 	while (f.readln(buf))
> 	{
> 		write(buf);
> 		outBuf ~= buf;
> 	}
>
> 	writeln("\n**** BUFFERED OUTPUT *****");
>
> 	foreach (line; outBuf)
> 	{
> 		write(line);
> 	}
> }
>
> testData.txt is just a couple of lines of miscellaneous text. 
> The expectation is that the raw output and the buffered output 
> should be exactly the same... but they are not. (If anyone 
> would like to see this for themselves, I stuck it in github: 
> https://github.com/MrTact/CharBug.)
>
> Changing the types of outBuf and buf to dchar works as 
> expected. Changing outBuf to a string[] and appending buf.idup 
> does as well.

Just outBuf ~= buf.dup; works, too. Without .dup you're 
overwriting and appending the same chunk of memory again and 
again.
 From the documentation on File.readln
(<http://dlang.org/phobos/std_stdio#readln>): "Note that reusing 
the buffer means that the previous contents of it has to be 
copied if needed."
I'm a bit puzzled as for why it behaves differently with dchar.


More information about the Digitalmars-d mailing list