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
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With