www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Random Access I/O

reply Chris Williams <yoreanon-chrisw yahoo.co.jp> writes:
I need to be able to perform random access I/O against a file, 
creating a new file if it doesn't exist, or opening as-is (no 
truncation) if it already exists.

None of the access modes for std.stdio.File seem to allow that. 
Any usage of the "w" mode causes my code to consider the file 
empty if it pre-exists (though, it doesn't always actually 
truncate the disk on file?)

If I was coding in C, I would use open() as it gives more options 
for access:

http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html

However, I don't see this exposed in phobos anywhere?
Mar 25 2016
next sibling parent data pulverizer <data.pulverizer gmail.com> writes:
On Saturday, 26 March 2016 at 00:10:23 UTC, Chris Williams wrote:
 I need to be able to perform random access I/O against a file, 
 creating a new file if it doesn't exist, or opening as-is (no 
 truncation) if it already exists.

 None of the access modes for std.stdio.File seem to allow that. 
 Any usage of the "w" mode causes my code to consider the file 
 empty if it pre-exists (though, it doesn't always actually 
 truncate the disk on file?)

 If I was coding in C, I would use open() as it gives more 
 options for access:

 http://pubs.opengroup.org/onlinepubs/009695399/functions/open.html

 However, I don't see this exposed in phobos anywhere?
The Programming in D book chapter on Files http://ddili.org/ders/d.en/files.html will help. I think the "std.stdio.File struct" section on the same page has what you need. Also, take a look at http://dlang.org/phobos/std_stdio.html#.File.open.
Mar 25 2016
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Saturday, 26 March 2016 at 00:10:23 UTC, Chris Williams wrote:
 None of the access modes for std.stdio.File seem to allow that. 
 Any usage of the "w" mode causes my code to consider the file 
 empty if it pre-exists (though, it doesn't always actually 
 truncate the disk on file?)
Mode "a+" or "r+" for append+read or read+write will do it, same as in C.
 If I was coding in C, I would use open() as it gives more 
 options for access:
This function is in `import core.sys.posix.fcntl;` (the documentation is lax on this but generally when you see a Posix function in C that is #include<x.h>, you can get it in D with `import core.sys.posix.X`.
Mar 25 2016