How do I create a new file using phobos ?
Martin Fuchs
martin-fuchs at gmx.net
Sat Sep 22 14:54:10 PDT 2007
>> Ex:
>> char[][] lines;
>> ...
>> {
>> scope Stream file = new BufferedFile("sample.txt",FileMode.Out);
>> foreach(line; lines) {
>> file.writefln(line);
>> }
>> }
>
> Wait a minute, can anyone confirm that it's ok to use a scope BufferedFile
> without explicitly closing?
> BufferedFile contiains a 'new File()', but no destructor. The File's
> destructor contains a close(), however. So will the member File's
> destructor get called when the scope BufferedFile goes out of scope?
>
> It's really just a question of how scope works w.r.t classes with heap
> allocated members. Do the reference members get deleted deterministically
> on scope exit too?
>
> --bb
This should be the correct solution to automatically close the file used in
a BufferedFile stream when leaving the function:
char[][] lines;
...
{
scope File file = new File("sample.txt", FileMode.Out);
Stream out = new BufferedFile(file);
foreach(line; lines) {
file.writefln(line);
}
}
Regards,
Martin
More information about the Digitalmars-d
mailing list