Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D file I/O functions

Tags:

file

io

d

I'm just learning D. Looks like a great language, but I can't find any info about the file I/O functions. I may be being dim (I'm good at that!), so could somebody point me in the right direction, please? Thanks

like image 862
Graham Nicholls Avatar asked Sep 16 '10 14:09

Graham Nicholls


People also ask

What are file I O functions?

Input/Output operations such as open, close, read, write and append, all of which deal with standard disk or tape files. The term would be used to refer to regular file operations in contrast to low-level system I/O such as dealing with virtual memory pages or OS tables of contents.

What are the I O functions available in C for binary files?

Binary I/O Functions size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); Both of these functions should be used to read or write blocks of memories - usually arrays or structures.


1 Answers

Basically, you use the File structure from std.stdio.

import std.stdio;

void writeTest() {
    auto f = File("1.txt", "w");        // create a file for writing,
    scope(exit) f.close();              //   and close the file when we're done.
                                        //   (optional)
    f.writeln("foo");                   // write 2 lines of text to it.
    f.writeln("bar");
}

void readTest() {
    auto f = File("1.txt");             // open file for reading,
    scope(exit) f.close();              //   and close the file when we're done.
                                        //   (optional)
    foreach (str; f.byLine)             // read every line in the file,
      writeln(":: ", str);              //   and print it out.
}

void main() {
   writeTest();
   readTest();
}
like image 86
kennytm Avatar answered Nov 09 '22 09:11

kennytm