www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Opening, reading and writing to files in binary mode

reply Mitja Ursic <Mitja_member pathlink.com> writes:
Does D language or any of its libraries support opening, reading and writing to
binary files in binary mode?

Cheers,
Mitja
Mar 20 2005
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Mitja Ursic" <Mitja_member pathlink.com> wrote in message 
news:d1kujb$1g86$1 digitaldaemon.com...
 Does D language or any of its libraries support opening, reading and 
 writing to
 binary files in binary mode?
IMO there isn't much reason to distinguish between "binary" and "text" file modes with modern OSes. You can certainly accomplish what you need to do with std.stream.File. There is no need to open a file for binary or text mode - you just write. import std.stream; void main() { File f=new File("test.bin", FileMode.OutNew); int x=5; float y=10.3; f.write(x); f.write(y); f.writeLine("This is regular text, even though there is no text file mode"); }
Mar 20 2005
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
Jarrett Billingsley wrote:
 IMO there isn't much reason to distinguish between "binary" and "text" file 
 modes with modern OSes.
So? What takes the burden of checking that line ends correspond to the platform? Let me remind you of the line end conventions: C and derivant languages: "\n" Unix: "\n" Windows: "\r\n" MacOS (including OSX): "\r" So, there are following options: * do no conversions - so does C library in binary mode - the user must do them if he needs to. A variant of this option would be to provide abstractions for EOL input and output, and requiere the user to make use of them whenever he outputs text - which would break normal C-style text handling though and limit the usefulness of C libraries. * convert different or plaform-specific line end sequences to "\n" on input, and back to platform-standard from "\n" on the output. Naturally, this will totally garble the binary output. So does C library in text mode. * always provide 2 accesses into the same stream - one for binary IO which would not garble, and the one for text IO which can modify the input and output. -eye
Mar 21 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Ilya Minkov" <minkov cs.tum.edu> wrote in message 
news:d1nd4e$p2r$1 digitaldaemon.com...
 So? What takes the burden of checking that line ends correspond to the 
 platform?
That would be std.stream.readLine()'s burden. "Read a line that is terminated with *some combination of carriage return and line feed* or end-of-file. " From the phobos doc. Note the starred text ;)
Mar 21 2005
prev sibling parent J C Calvarese <jcc7 cox.net> writes:
Mitja Ursic wrote:
 Does D language or any of its libraries support opening, reading and writing to
 binary files in binary mode?
Sure. The std.stream module shouldn't have a problem doing that. This example shows how to extract a file from a .zip archive: http://www.dsource.org/tutorials/index.php?show_example=121 (The std.file module was used to read the file, but I'm sure std.stream can read binary files, too.) -- Justin (a/k/a jcc7) http://jcc_7.tripod.com/d/
Mar 20 2005