We will learn to read and write files from C++ in this chapter.
To do that, first of all we need to include a header file from Standard C++ Library named fstream.
fstream is combined with two data separate library files which are:
So, before we input some text within a text file, we need to open that file.
And after that we can write something on that file.
Example:
Opening a file by providing a path:
Instead of creating a new object and calling open function from ofstream library we can directly specify the file directory path by using ofstream object constructor.
}
There are various open modes which are used to indicate, what to do the file (eg. open, close) and the data (eg. Read, write). Some of the modes are described below:
Basic codes for file processing:
To do that, first of all we need to include a header file from Standard C++ Library named fstream.
fstream is combined with two data separate library files which are:
· ofstream – Output file stream that creates and writes information to files.
· ifstream - Input file stream that read information from files.
So, before we input some text within a text file, we need to open that file.
And after that we can write something on that file.
Example:
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream NewFile;
NewFile.open("example.txt”);
NewFile<<"New Line added\n";
NewFile.close();
return 0;
}
· This program will include fstream as header file.
· Then it calls ofstream to create a new file. The file will be opened or created using open function.
· And then it names that file within parentheses and double inverted commas (If a file with the same name is already stored in the project directory then it will open that one).
· After that it writes something on that txt file and save it and then it closes that file using close function.
Opening a file by providing a path:
Instead of creating a new object and calling open function from ofstream library we can directly specify the file directory path by using ofstream object constructor.
int main(){
ofstream NewFile("f:\\example.txt");
NewFile<<"New Line added";
NewFile.close();
return 0;
}
There are various open modes which are used to indicate, what to do the file (eg. open, close) and the data (eg. Read, write). Some of the modes are described below:
Basic codes for file processing:
- To read a file which is already existed:
- To write something in a newly opened file, or a file which is already existed:
- To open a file in the same project folder for reading (Here example.txt is the filename):
- To open a file in the different folder for reading (Here example.txt is the filename):
- To opening a file for reading in binary type modes, we could write:
- To open a file and then seek to the end of that file, we could write like this:
- To open a file for writing and appending at the end of the output file, we could write like this:
- To check that the given name is already existed for a file within the same folder, we could write like this:
- To free up the resources to be used by other processes or programs. Using the close() member function to close a function declared already, for input stream we would write like this:
- And for output stream:
· Here FileRead and FileWrite are just objects to call functions, you can change them to any suitable word.