Post By: Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------
D - 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.
Opening Files
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 will use to name your file and access mode can have one of the following values:
Mode | Description |
---|---|
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. |
Closing a File
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 automatically.
Writing a File
file.writeln is used to write to a 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's working directory).
Reading a File
Following is the simple method to read 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.
0 comments:
Post a Comment