2 File IO Questions (Phobos)

Zane zane.sims at gmail.com
Sat Nov 7 03:20:47 PST 2009


Zane Wrote:

> Hello all,
> 
> I have been looking at Phobos lately, and currently I am having some trouble understanding what is going on with the following 2 trivial examples.  First of all, I am using dmd v1.050.
> 
> This one prints "[‼   This is some stuff!]".  Where do the 4 prepended bytes come from?
> (1) ---------------------------------------------------------------->
> 
> import std.stream;
> import std.stdio;
> 
> int main()
> {
> 	char[] stuff = "This is some stuff!";
> 
> 	File f = new File("stuff.txt", FileMode.Out | FileMode.In);
> 	f.write(stuff);
> 	f.seekSet(0);
> 	stuff = f.readLine();
> 	writef("[%s]", stuff);
> 	f.close;
> 
> 	return 0;
> }
> 
> 
> This one I wanted to have a class open a file upon initialization of an instance, and close a file when the destructor is called.  I get an "Error: Access Violation" unless I comment out the file.close line.  Why?  (of course this example also has the same problem as the first example, but I kept the first one simpler to narrow down things)
> (2) ---------------------------------------------------------------->
> 
> import std.stream;
> 
> public class StuffWriter
> {
> 	File file;
> 
> 	this(char[] filename)
> 	{
> 		file = new File(filename, FileMode.Out);
> 	}
> 
> 	~this()
> 	{
> 		file.close; //this causes an access violation???
> 	}
> 
> 	public void write(char[] stuff)
> 	{
> 		file.write(stuff);
> 	}
> }
> 
> int main()
> {
> 	StuffWriter sw = new StuffWriter("stuff.txt");
> 	sw.write("This is some stuff!");
> 
> 	return 0;
> }
> 
> 
> Thanks!
> Zane

Doh! I still need help with number 2, but for number 1, all I needed was to use 'writeString' instead of 'write'.  Like I said, still getting used to Phobos.  Like I said, I still need help on the destructor question (#2).

Thanks,
Zane


More information about the Digitalmars-d-learn mailing list