Segmentation fault on closing file in destructor
Lars T. Kyllingstad
public at kyllingen.NOSPAMnet
Mon Sep 27 04:19:36 PDT 2010
On Sun, 26 Sep 2010 20:55:33 +0200, Tom Kazimiers wrote:
> If I would use std.stdio.File, what would be different?
Well, for one thing you won't have to write your code all over again when
std.stream is deprecated, which will happen soon. std.stdio.File is
really what you should use for file I/O in new code.
That said, there's a chance it does exactly what you want. You don't
have to open a file on construction, there's an open() function which
opens a file and assigns it to the File handle. Nor do you have to worry
about closing the file in the destructor, as it is automatically closed
the moment the last reference to it goes out of scope.
Here are a few examples of how to use it:
File file; // File's a struct, so no need to use 'new'
// Read a text file line by line
file.open("foo.txt");
foreach (line; file.byLine()) writeln(line);
// Read a binary file in 4MB chunks
file.open("foo.dat");
foreach (ubyte[] chunk; file.byChunk(4*1024))
doStuffWith(chunk);
// Read up to 100 ints from a file
file.open("myInts");
auto buffer = new int[100];
auto data = file.rawRead(buffer);
-Lars
More information about the Digitalmars-d-learn
mailing list