BufferedFile bug?

Regan Heath regan at netmail.co.nz
Thu Mar 6 03:58:48 PST 2008


Hey,

[DMD 2.010, windows]

So.. was doing a bit of simple D coding and got some really odd 
behaviour, namely this code creates a 4gb (4,294,975,895 bytes) file on 
my disk!

My question is, is this a bug or am I doing something stupid?

The idea is to open a file and overwrite random blocks with '0' (the 
ascii number, not the decimal value - though I get the same behaviour 
with either).

When executed this code seems to hang after outputting "Close.." and at 
that point the file is growing, and growing... ctrl+c does nothing 
immediately but seems to work after the file reaches 4gb, at a guess 
when close actually returns.

Opening the file in textpad only shows 8232 characters, all 'a' - so 
none of the modifications have been applied and it seems an eof 
character has been inserted at that point in the 4gb file.

Adding a return after the file creation before the modification shows a 
file which is as expected 50000 characters long, all 'a'.  So that at 
least is working as expected.

import std.stdio;
import std.stream;
import std.file;
import std.random;

int main()
{
	// Create small file
	auto 	f = new BufferedFile("test", FileMode.OutNew);
	int	size = 50000;
	
	for(int i = 0; i < size; i++)
		f.write('a');
		
	f.close;

	// Open and modify	
	f = new BufferedFile("test", FileMode.In | FileMode.Out);
	ulong	total = getSize("test");
	long	block = total/1000;
	long 	offset;
	long	bytes;
	Random 	gen;
	
	writefln("Total: %d,%d", total, block);
	
	for(int i = 0; i < 2; i++)
	{
		offset = uniform!(long)(gen, 0L, block);
		bytes  = uniform!(long)(gen, 0L, block);
		
		writefln("Seek : %d,%d,%d", f.position, offset, bytes);
		
		f.seekCur(offset);
		
		writefln("Mod  : %d,%d", f.position, bytes);
		
		for(long j = 0; j < bytes; j++)
			f.write('0');
			
		writefln("Done : %d", f.position);
	}
	
	writefln("Close: %d", f.position);
	
	f.close;
	
	writefln("Done");
	
	return 0;
}



More information about the Digitalmars-d mailing list