D Programming - File I/O

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.
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 literal, which you use to name the file and access mode can have one of the following values −
r
Opens an existing text file for reading purpose.
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.
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.
r+
Opens a text file for reading and writing both.
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.
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.
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 explicitly; they are closed automatically when File objects are terminated.
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).
The following method reads a single line 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 line = chomp(file.readln()); 
      writeln("line -", line); 
   }
} 
When the above code is compiled and executed, it reads the file created in previous section and produces the following result −
line -hello 
line -world 
line -

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

    10 Lectures 
     1.5 hours 

 Nishant Malik

    42 Lectures 
     1.5 hours 

 Ravi Kiran

    46 Lectures 
     7.5 hours 

 Aditya Dua

    32 Lectures 
     3.5 hours 

 Sharad Kumar

    14 Lectures 
     1 hours 

 Mahesh Kumar

    18 Lectures 
     49 mins

 Rushi Panchal
 Print
 Add Notes
 Bookmark this page