Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views3 pages

File Handling

The document provides C++ code examples for file operations including writing to a file, reading from a file, appending to a file, checking if a file exists and clearing its contents, and reading/writing binary files with structures. Each section includes a brief explanation of the code functionality and demonstrates the use of file streams. The examples cover basic file handling techniques essential for managing file input and output in C++.

Uploaded by

chattaanply
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)
7 views3 pages

File Handling

The document provides C++ code examples for file operations including writing to a file, reading from a file, appending to a file, checking if a file exists and clearing its contents, and reading/writing binary files with structures. Each section includes a brief explanation of the code functionality and demonstrates the use of file streams. The examples cover basic file handling techniques essential for managing file input and output in C++.

Uploaded by

chattaanply
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/ 3

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, this is a test file.\n";
file << "This is line 2.\n";
file.close(); // Close the file
cout << "File written successfully.\n";
} else {
cout << "Error opening file.\n";
}

return 0;
}

2. Reading from a File


#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 << "Unable to open file.\n";
}

return 0;
}
3. Append to a File
#include <iostream>
#include <fstream>
using namespace std;

int main() {
ofstream file("example.txt", ios::app); // Open in append mode

if (file.is_open()) {
file << "This is an appended line.\n";
file.close();
cout << "Line appended successfully.\n";
} else {
cout << "Error opening file.\n";
}

return 0;
}

4. Check if File Exists and Clear Contents


#include <iostream>
#include <fstream>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;

int main() {
string filename = "example.txt";

if (fs::exists(filename)) {
cout << filename << " exists. Clearing contents...\n";
ofstream file(filename, ios::trunc); // Open in truncate mode
file.close();
} else {
cout << filename << " does not exist.\n";
}

return 0;
}
5. Read and Write with Structures (Binary Files)
#include <iostream>
#include <fstream>
using namespace std;

struct Student {
char name[50];
int age;
float gpa;
};

int main() {
Student s1 = {"Alice", 20, 3.8};

// Write binary
ofstream outFile("student.dat", ios::binary);
outFile.write(reinterpret_cast<char*>(&s1), sizeof(s1));
outFile.close();

// Read binary
Student s2;
ifstream inFile("student.dat", ios::binary);
inFile.read(reinterpret_cast<char*>(&s2), sizeof(s2));
inFile.close();

cout << "Student: " << s2.name << ", Age: " << s2.age << ", GPA: " <<
s2.gpa << endl;

return 0;
}

You might also like