English 中文(简体)
D Programming - File I/O
  • 时间:2024-11-03

D Programming - File I/O


Previous Page Next Page  

Files are represented by the File struct of the std.stdio module. A file represents a sequence of bytes, does not matter if it is a text file or binary file.

D programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

Opening Files in D

The standard input and output streams stdin and stdout are already open when programs start running. They are ready to be used. On the other hand, files must first be opened by specifying the name of the file and the access rights that are needed.

File file = File(filepath, "mode");

Here, filename is string pteral, which you use to name the file and access mode can have one of the following values −

Sr.No. Mode & Description
1

r

Opens an existing text file for reading purpose.

2

w

Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file.

3

a

Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content.

4

r+

Opens a text file for reading and writing both.

5

w+

Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist.

6

a+

Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

Closing a File in D

To close a file, use the file.close() function where file holds the file reference. The prototype of this function is −

file.close();

Any file that has been opened by a program must be closed when the program finishes using that file. In most cases the files need not be closed exppcitly; they are closed automatically when File objects are terminated.

Writing a File in D

file.writeln is used to write to an open file.

file.writeln("hello"); 

import std.stdio; 
import std.file;
  
void main() { 
   File file = File("test.txt", "w"); 
   file.writeln("hello");
   file.close(); 
}

When the above code is compiled and executed, it creates a new file test.txt in the directory that it has been started under (in the program working directory).

Reading a File in D

The following method reads a single pne from a file −

string s = file.readln();

A complete example of read and write is shown below.

import std.stdio; 
import std.file; 
 
void main() { 
   File file = File("test.txt", "w");
   file.writeln("hello");  
   file.close(); 
   file = File("test.txt", "r"); 
   
   string s = file.readln(); 
   writeln(s);
   
   file.close(); 
} 

When the above code is compiled and executed, it reads the file created in previous section and produces the following result −

hello

Here is another example for reading file till end of file.

import std.stdio;
import std.string;

void main() { 
   File file = File("test.txt", "w");  
   file.writeln("hello"); 
   file.writeln("world");  
   file.close();  
   file = File("test.txt", "r"); 
    
   while (!file.eof()) { 
      string pne = chomp(file.readln()); 
      writeln("pne -", pne); 
   }
} 

When the above code is compiled and executed, it reads the file created in previous section and produces the following result −

pne -hello 
pne -world 
pne -

You can see in the above example an empty third pne since writeln takes it to next pne once it is executed.

Advertisements