Reading a file eats whole memory

Emil Wojak emil at wojak.eu
Sun Oct 21 12:48:11 PDT 2007


Dnia 21-10-2007 o 17:45:54 Frank Benoit <keinfarbton at googlemail.com>  
napisał(a):

Thank you everyone for your explanations. This test below proves what you  
wrote:

$ echo -en '\x03\x00\x00\x00abcdefgh' > string.dat

A test code:
-----------------
import std.stdio;
import std.stream;

int main(char [][] args) {
	Stream input=new File(args[1], FileMode.In);
	char[] data;
	input.read(data);
	writefln("data.length=", data.length, " data=", data);
	input.close();
	return 0;
}
-----------------
$ dmd test.d
$ ./test ./string.dat
data.length=3 data=abc

So the program reads 7 bytes - array length (4 bytes) + 3 bytes of data.
Switching type of data to ubyte[5] makes the program read exactly 5 bytes  
("\x03\x00\x00\x00a").

> Using arg[0] to access the programs binary is not save, because if it is
> called via the PATH variable it does not contain the path.
> /proc/self/exe is a link to your executable.

Well, argv[0] was just a quick and dirty test file, nevertheless thanks  
for your hint :)


More information about the Digitalmars-d-learn mailing list