


Khusboo Tayal
#include <fstream>
ofstream file;
file.open("example.txt");
file.close();
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
file.open("example.txt"); // Opening file for writing
file << "Hello, C++ File Handling!" << endl; // Writing to file
file.close(); // Closing file
cout << "File created and text written." << endl;
return 0;
}
File created and text written.
Hello, C++ File Handling!
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt");
if (file.is_open()) {
file << "This is line 1.\n";
file << "This is line 2.\n";
file << "This is line 3.\n";
file.close();
cout << "Text written to file." << endl;
} else {
cout << "Failed to open file." << endl;
}
return 0;
}
Text written to file.
This is line 1. This is line 2. This is line 3.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("example.txt");
string line;
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cout << "Failed to open file." << endl;
}
return 0;
}
This is line 1. This is line 2. This is line 3.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("example.txt");
string content;
if (file.is_open()) {
content.assign((istreambuf_iterator<char>(file)),
(istreambuf_iterator<char>()));
cout << "File Content:\n" << content << endl;
file.close();
} else {
cout << "Failed to open file." << endl;
}
return 0;
}
File Content: This is line 1. This is line 2. This is line 3.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("example.txt", ios::app); // Append mode
file << "This is an appended line.\n";
file.close();
cout << "Data appended to file." << endl;
return 0;
}
This is line 1. This is line 2. This is line 3. This is an appended line.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream file("example.txt", ios::in | ios::out);
// Writing at specific position
file.seekp(15); // Move to 15th character position
file << "Inserted Text";
// Reading from specific position
file.seekg(0); // Move to beginning
string content;
while (getline(file, content)) {
cout << content << endl;
}
file.close();
return 0;
}
This is line 1. Inserterted Textine 2. This is line 3. This is an appended line.
You should always check if the file was successfully opened:
ifstream file("nonexistent.txt");
if (!file) {
cerr << "Error: File could not be opened." << endl;
}Binary files store data in a raw format (not human-readable).
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file("data.bin", ios::binary);
int number = 12345;
file.write(reinterpret_cast<char*>(&number), sizeof(number));
file.close();
cout << "Binary data written." << endl;
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("data.bin", ios::binary);
int number;
file.read(reinterpret_cast<char*>(&number), sizeof(number));
file.close();
cout << "Binary data read: " << number << endl;
return 0;
}
Binary data read: 12345
In this tutorial, you learned: