File Opening Mode in C++
A file in C++ is a collection of bytes kept on a storage medium, such as a hard drive. Data is stored and retrieved using files, offering a mechanism to persistently save data past the end of a program's execution.
Text files and binary files are the two primary file formats in C++.
Text Files:
Human-readable data is stored in text files. Each character is encoded using a particular character encoding, such as ASCII or UTF-8, composed of a string of characters. For example, configuration files, log files, and plain text documents are frequently stored in text files. Using the std::ifstream (input file stream) and std::ofstream (output file stream) classes from the header, you can read from and write to text files in C++.
Binary files:
Without particular character encoding, binary files hold data in unprocessed binary form. Images, audio files, serialized objects, and other sophisticated data structures are stored using them. Any series of bytes, including control and non-printable characters, can be found in binary files. The ‘std::ifstream’ and ‘std::ofstream’ classes in C++ may be used to read from and write to binary files, much like you do with text files. The ‘std::ios::binary’ option must be included to open the file in binary mode.
The ‘std::fstream’ class in C++ can interact with files. Choose the file opening mode when opening a file to control how it will be accessed. The <fstream> header defines some flags as constants that specify the file opening mode together.
The typical C++ file opening modes are listed below:
1. std::fstream::in:
The std::fstream class in C++ uses the open mode flag std::fstream::in to indicate that a file should be opened for input operations.
The C++ Standard Library's <fstream> header contains the std::fstream class, which offers capabilities for reading from and writing to files. You may define the open mode by utilizing flags like std::fstream::in, std::fstream::out, std::fstream::binary, and more when opening a file with std::fstream.
2. std::fstream::out:
The std::fstream class in C++ uses the open mode option std::fstream::out to indicate that a file should be opened for output operations. The C++ Standard Library's <fstream> header contains the std::fstream class, which offers capabilities for reading from and writing to files. You may define the open mode by utilizing flags like std::fstream::in, std::fstream::out, std::fstream::binary, and more when opening a file with std::fstream.
3. std::fstream::app:
To request that a file be opened in append mode in C++, use the std::fstream::app open mode flag with the std::fstream class. When a file is opened using the std::fstream::app function, additional information will be added to the file's existing content rather than being overwritten.
4. std::fstream::binary:
In C++, std::fstream::binary is an open-mode flag used with the std::fstream class to specify binary mode when opening a file. When a file is opened with std::fstream::binary, it is treated as a binary rather than a text file.
5. std::fstream::ate:
The std::fstream class in C++ uses the open mode option std::fstream::ate to place the file pointer at the end of the file as soon as it is opened. The word "ate" means "at the end."
6. std::fstream::trunc:
In C++, std::fstream::trunc is an open-mode flag used with the std::fstream class to truncate the contents of a file when it is opened. The name "trunc" stands for "truncate."
When a file is opened with std::fstream::trunc, the existing content of the file is deleted, and the file is effectively cleared before any new data is written to it.
Combine these modes using the bitwise OR operator (|) to get the desired file opening behavior. You can use the commands std::ios::in | std::ios::out to open a file for reading and writing. It's crucial to remember that the input/output streams' base class, std::ios, also includes the file opening modes. The file opening modes and file stream classes work together to access files for specific purposes, such as std::ifstream, std::ofstream, or std::fstream.
Here is an example of C++ code that shows how to use the fstream library to open a file in various modes:
Code:
#include <fstream>
#include <iostream>
int main() {
// Open a file for writing
std::ofstream outputFile("output.txt");
if (outputFile.is_open()) {
outputFile << "Hello, World!\n";
outputFile.close();
std::cout << "File written successfully.\n";
} else {
std::cout << "Failed to open the file for writing.\n";
}
// Open a file for reading
std::ifstream inputFile("input.txt");
if (inputFile.is_open()) {
std::string line;
while (std::getline(inputFile, line)) {
std::cout << line << '\n';
}
inputFile.close();
} else {
std::cout << "Failed to open the file for reading.\n";
}
// Open a file for appending
std::ofstream appendFile("append.txt", std::ios::app);
if (append file.is_open()) {
append file << "New content appended!\n";
append file.close();
std::cout << "File appended successfully.\n";
} else {
std::cout << "Failed to open the file for appending.\n";
}
return 0;
}
Output:
![File Opening Mode in C++](https://static.tutorialandexample.com/cpp/file-opening-mode-in-cpp1.png)
In the code above, we first use std::ofstream to open a file called "output.txt" for writing. We add a line of text to the file if it can be successfully opened. The file is then closed, and a success message is printed. A notice is presented if the file cannot be opened.
Next, we use std::ifstream to open the "input.txt" file for reading. We read the file's contents line by line and print each line to the terminal if it opens correctly. We close the file at this point.
Finally, we open the "append.txt" file using the std::ofstream and std::ios::app modes. If the file is opened successfully, a new line of text is added, the file is closed, and a success message is printed. A notice is presented if the file cannot be opened.
You can manage how files are opened and handled in your C++ programs by being aware of and adequately utilizing the file opening modes.