0% found this document useful (0 votes)
4 views4 pages

File Handling

File handling in C++ utilizes the <fstream> library, which includes ifstream for reading, ofstream for writing, and fstream for both. The document provides examples of writing to a file, reading from a file, and taking user input to store in a file. Each example demonstrates the importance of checking if the file is open and closing the file after operations.

Uploaded by

Daud Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

File Handling

File handling in C++ utilizes the <fstream> library, which includes ifstream for reading, ofstream for writing, and fstream for both. The document provides examples of writing to a file, reading from a file, and taking user input to store in a file. Each example demonstrates the importance of checking if the file is open and closing the file after operations.

Uploaded by

Daud Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

File Handling in C++

File handling in C++ allows programs to read from and write to files. This is done using the
standard C++ library <fstream>, which provides three main classes:

File Stream Classes:

Class Purpose

ifstream Input file stream (for reading files)

ofstream Output file stream (for writing files)

fstream File stream for both input and output

Example 1: Writing to a File:


#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream file("example.txt"); // Create and open a file
if (file.is_open()) {
file << "Hello, world!\n";
file << "This is a file handling example.";
file.close(); // Always close the file
} else {
cout << "Unable to open file for writing.\n";
}
return 0;
}
Output:
Example 2: Reading from a File:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {
ifstream file("example.txt"); // Open the file to read
string line;
if (file.is_open()) {
while (getline(file, line)) {
cout << line << endl;
}
file.close();
} else {
cout << "Unable to open file for reading.\n";
}
return 0;
}

Example 3: Take Input from User and Store in a File


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string data;
ofstream file("userdata.txt"); // Create or open the file for writing

if (file.is_open()) {
cout << "Enter your name: ";
getline(cin, data); // Get full line including spaces
file << "Name: " << data << endl;

cout << "Enter your age: ";


getline(cin, data);
file << "Age: " << data << endl;

cout << "Enter your address: ";


getline(cin, data);
file << "Address: " << data << endl;

file.close(); // Close the file


cout << "Data successfully saved to 'userdata.txt'." << endl;
} else {
cout << "Error opening file!" << endl;
}

return 0;
}
Output:

You might also like