Files in C++ are used to store and retrieve data permanently from the hard disk or other storage media.
To work with file in C++, you need to include the fstream header file. This header provides two classes that you can use to work with file: ifstream and ofstream. The ifstream class is used for reading data from a file, while the ofstream class is used for writing data to a file.
Here’s an example of how to open a file for reading using ifstream:
#include
#include
int main() {
std::ifstream inputFile("example.txt");
if (inputFile.is_open()) {
std::cout << "File opened successfully!" << std::endl;
// Use the file for reading
inputFile.close();
} else {
std::cout << "Error opening file" << std::endl;
}
return 0;
}
In this example, we create an ifstream object called inputFile and pass the filename “example.txt” to its constructor. We then check if the file was opened successfully using the is_open() method. If it was, we can use the file for reading. Once we’re done reading from the file, we call the close() method to close the file.
Similarly, to open a file for writing using ofstream, we can do the following:
#include
#include
int main() {
std::ofstream outputFile("example.txt");
if (outputFile.is_open()) {
std::cout << "File created successfully!" << std::endl;
// Use the file for writing
outputFile.close();
} else {
std::cout << "Error creating file" << std::endl;
}
return 0;
}
In this example, we create an ofstream object called outputFile and pass the filename “example.txt” to its constructor. We then check if the file was created successfully using the is_open() method. If it was, we can use the file for writing. Once we’re done writing to the file, we call the close() method to close the file.
C++ provides the following three types of files:
Text : Text files are used to store characters in an organized manner, usually for human-readable text. It can be easily created and edited with a simple text editor. In C++, text files are accessed using fstream class objects. Here’s an example of creating a text file and writing data to it:
#include
#include
using namespace std;
int main() {
ofstream file("mytextfile.txt");
file << "This is some text in my file.\n";
file.close();
return 0;
}Top of Form
#include
#include
using namespace std;
int main() {
ofstream file("mybinaryfile.bin", ios::binary);
int data[5] = {1, 2, 3, 4, 5};
file.write((char*)&data, sizeof(data));
file.close();
return 0;
}
3 Random access : These files allow you to access the data in a file in a non-sequential manner. This means that you can read and write data at any point in the file, rather than just at the beginning or end. These are useful when you need to read or modify specific parts of a file without having to read or write the entire file.
#include
#include
using namespace std;
struct Student {
char name[20];
int age;
};
int main() {
fstream file("students.dat", ios::in | ios::out | ios::binary);
if (!file) {
cerr << "Error opening file!" << endl;
return 1;
}
// Write some data to the file
Student s1 = { "John", 20 };
file.write(reinterpret_cast(&s1), sizeof(s1));
// Seek to the beginning of the file
file.seekg(0);
// Read the data from the file
Student s2;
file.read(reinterpret_cast(&s2), sizeof(s2));
// Modify the data in the file
s2.age = 21;
file.seekp(0);
file.write(reinterpret_cast(&s2), sizeof(s2));
file.close();
return 0;
}
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.