Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

File IO Problems

Moderators: larsivi kris

Posted: 11/17/08 04:14:57

Hello

Where is the problem on this program?

/// write a binary file with three integers

auto dfo = new DataFileOutput(new FileOutput("io.dat"));
	
dfo.putInt(1);
dfo.putInt(2);
dfo.putInt(3);
	
dfo.flush();
dfo.close();

/// read the same file

auto dfi = new DataFileInput(new FileInput("io.dat"));
	
dfi.getInt(); // get 1
dfi.seek(int.sizeof, FileConduit.Anchor.Current); // skip 2
Stdout.formatln("{}", dfi.getInt()); // get 3
		
dfi.close();

i suppose this should print 3, but throws an IOException: "end-of-flow whilst reading"

Thanks

Author Message

Posted: 11/17/08 09:27:18

Yes, it should print 3 - there is something bogus with the Anchor.Current, at least on Linux. If you seek to 2 * int.sizeof using the default anchor, you get the behaviour you expect. Also, printing out dfi.file.position after reading one int, shows that the Anchor.Current seeking is problematic (it is used with offset 0 to find the position). I would assume that some definition is wrong somewhere inside Tango, but please create a ticket for it? If you have time, feel free to dig into the problem too :)

Posted: 11/17/08 14:53:21

The problem is that DataFileStream is buffered, but the seek function does not take into account the loaded buffer.

If the anchor is current, the seek function should try and seek within the buffer, and if that isn't possible, it should subtract the number of bytes that are readable in the buffer from the position given before calling the conduit's seek.

There may be other seekable streams that have this problem...

Posted: 11/17/08 19:22:22 -- Modified: 11/17/08 19:23:11 by
schveiguy

per Kris' approval, I checked in a fix for this in trunk [4087]

And FYI, there is no other seekable streams, so this problem doesn't affect anything else.

Posted: 11/18/08 00:38:41

Thanks for the fix and responses... i have looking for the error, and also it can be found in FileConduit?, because it haves a seek function, but it can not update the buffer of any data stream (DataInput?, DataFileInput?)...

Posted: 11/18/08 13:43:32

FileConduit is not buffered by default, so it shouldn't have that problem.

DataStream might not have a seekable conduit as it's conduit, so it cannot be smart enough to implement seek. If you want to use DataStream with a file conduit and have the ability to seek, use DataFileStream like you were using it. With the new fix, this should work properly.

However, it is a common requirement to have a buffered FileConduit, so it might be worth looking at adding a seekable buffer. I'll add a ticket and see where it leads.