File manipulation is a crucial aspect of C++ programming. Many applications require interacting with files by either reading from or writing to them. The file handling library in C++ offers a range of functions to facilitate file operations such as opening, reading, writing, and closing files. This guide will demonstrate the process of opening and closing a file in C++.
File manipulation pertains to the act of reading from or writing to files. In C++, the file handling library can be employed for executing file operations. This library offers a variety of functions that enable us to open, read, write, and terminate files. File handling serves as a tool for carrying out activities like saving data, fetching data, and altering data within files.
File modes in C++:
In C++, files can be accessed in different modes based on the actions we intend to execute on them. Below are the frequently utilized file modes in C++:
- std::ios::in:
This option is chosen when there is a need to access a file solely for reading purposes. Once a file is opened in this mode, it allows for reading operations but restricts any writing capabilities.
C Code:
std::ifstream infile;
infile.open("myfile.txt", std::ios::in);
- std::ios::out:
This option is selected when there is a need to access a file for the purpose of writing exclusively. With this mode, it is permissible to add content to the file but retrieval of information from it is restricted. In cases where the specified file is absent, it will be generated automatically.
C Code:
std::ofstream outfile;
outfile.open("myfile.txt", std::ios::out);
- std::ios::app:
This option is selected when there is a need to open a file and add new data at the end of the existing content. In case the file is not present, a new file will be generated.
C Code:
std::ofstream outfile;
outfile.open("myfile.txt", std::ios::app);
- std::ios::ate:
This option is selected when there is a need to open a file and position the cursor at the end of it right away. This feature comes in handy when there is a requirement to add new information to the end of a file or to read the complete contents of the file in one go.
C Code:
std::ifstream infile;
infile.open("myfile.txt", std::ios::ate);
- std::ios::binary:
When we opt to open a file in binary mode, it signifies that no newline translation occurs, and the file is viewed as a series of bytes. Binary mode is commonly employed when handling non-textual files like images or executable programs.
C Code:
std::ifstream infile;
infile.open("myfile.bin", std::ios::binary);
Note: We can combine these modes using the bitwise OR operator (|). For example, to open a file for both reading and writing, we can use the following:
C Code:
std::fstream file;
file.open("myfile.txt", std::ios::in | std::ios::out);
When a file is opened in write mode, it's crucial to understand that the existing contents of the file will be replaced. To prevent this, we have the option to utilize the std::ios::app mode, which allows us to add data at the end of the file without overwriting it.
Opening a file in C++:
To access a file in C++, we have the option to utilize the ofstream and ifstream classes. The ofstream class serves the purpose of writing data to a file, whereas the ifstream class is employed for reading data from a file. These classes inherit from the fstream class, which offers functionality for both reading from and writing to files.
Here is a demonstration of how to initiate a file for composing content with the ofstream class:
C++ Code:
#include <fstream>
using namespace std;
int main() {
ofstream outfile;
outfile.open("example.txt", ios::out);
// Write to the file here
outfile.close();
return 0;
}
In this instance, we instantiate an instance of the ofstream class identified as " outfile ". Next, the open method is applied to access a file named " example.txt " in output mode (ios::out) . Subsequently, we have the ability to add content to the file utilizing the diverse output functions offered by the ofstream class. Upon completion of writing to the file, the close method is utilized to finalize the file operation.
Likewise, here is an illustration of how to initiate a file for reading utilizing the ifstream class:
C++ Code:
#include <fstream>
using namespace std;
int main() {
ifstream infile;
infile.open("example.txt", ios::in);
// Read from the file here
infile.close();
return 0;
}
In this instance, we instantiate an object of the ifstream class called " infile ". Following that, we employ the open method to access a file titled " example.txt " in input mode (ios::in) . Subsequently, we can retrieve data from the file utilizing the different input functions offered by the ifstream class. Upon completion of reading from the file, we employ the close method to finalize the file operation.