#C++ File I/O
#Overview
C++ provides comprehensive file I/O capabilities through the fstream library, supporting both text and binary file operations.
#Text File Operations
#Writing to Text Files
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Writing to a text file
std::ofstream outFile("example.txt");
if (!outFile) {
std::cout << "Error opening file for writing!" << std::endl;
return 1;
}
outFile << "Hello, World!" << std::endl;
outFile << "This is a C++ file I/O example." << std::endl;
outFile << "Number: " << 42 << std::endl;
outFile << "Pi: " << 3.14159 << std::endl;
outFile.close();
std::cout << "Data written to file successfully." << std::endl;
return 0;
}#Reading from Text Files
#include <iostream>
#include <fstream>
#include <string>
int main() {
// Reading from a text file
std::ifstream inFile("example.txt");
if (!inFile) {
std::cout << "Error opening file for reading!" << std::endl;
return 1;
}
std::string line;
std::cout << "File contents:" << std::endl;
// Read line by line
while (std::getline(inFile, line)) {
std::cout << line << std::endl;
}
inFile.close();
return 0;
}#Reading Formatted Data
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream inFile("data.txt");
if (!inFile) {
std::cout << "Error opening file!" << std::endl;
return 1;
}
std::string name;
int age;
double salary;
// Read formatted data
inFile >> name >> age >> salary;
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Salary: " << salary << std::endl;
inFile.close();
return 0;
}#Binary File Operations
#Writing Binary Files
#include <iostream>
#include <fstream>
#include <vector>
struct Person {
char name[50];
int age;
double salary;
};
int main() {
Person person = {"John Doe", 30, 50000.0};
// Write binary data
std::ofstream outFile("person.dat", std::ios::binary);
if (!outFile) {
std::cout << "Error opening binary file for writing!" << std::endl;
return 1;
}
outFile.write(reinterpret_cast<char*>(&person), sizeof(Person));
outFile.close();
std::cout << "Binary data written successfully." << std::endl;
return 0;
}#Reading Binary Files
#include <iostream>
#include <fstream>
struct Person {
char name[50];
int age;
double salary;
};
int main() {
Person person;
// Read binary data
std::ifstream inFile("person.dat", std::ios::binary);
if (!inFile) {
std::cout << "Error opening binary file for reading!" << std::endl;
return 1;
}
inFile.read(reinterpret_cast<char*>(&person), sizeof(Person));
inFile.close();
std::cout << "Name: " << person.name << std::endl;
std::cout << "Age: " << person.age << std::endl;
std::cout << "Salary: " << person.salary << std::endl;
return 0;
}#File Positioning
#Random Access
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::fstream file("random.txt", std::ios::in | std::ios::out | std::ios::binary);
if (!file) {
std::cout << "Error opening file!" << std::endl;
return 1;
}
// Write some data
file << "Hello, C++ File I/O!";
// Get current position
std::streampos pos = file.tellp();
std::cout << "Current position: " << pos << std::endl;
// Move to beginning
file.seekp(0, std::ios::beg);
// Overwrite first 5 characters
file.write("Hi!!!", 5);
// Move to specific position
file.seekp(7, std::ios::beg);
file.write("World", 5);
file.close();
// Read the modified file
std::ifstream inFile("random.txt");
std::string content;
std::getline(inFile, content);
std::cout << "Modified content: " << content << std::endl;
return 0;
}#File Stream States
#Checking File States
#include <iostream>
#include <fstream>
int main() {
std::ifstream inFile("nonexistent.txt");
// Check file state
if (!inFile) {
std::cout << "File could not be opened!" << std::endl;
if (inFile.fail()) {
std::cout << "Fail bit set" << std::endl;
}
if (inFile.bad()) {
std::cout << "Bad bit set" << std::endl;
}
}
// Clear error states
inFile.clear();
return 0;
}#Advanced File Operations
#Processing Large Files
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream inFile("large_file.txt");
if (!inFile) {
std::cout << "Error opening file!" << std::endl;
return 1;
}
// Get file size
inFile.seekg(0, std::ios::end);
std::streamsize fileSize = inFile.tellg();
inFile.seekg(0, std::ios::beg);
std::cout << "File size: " << fileSize << " bytes" << std::endl;
// Read entire file into buffer
std::vector<char> buffer(fileSize);
inFile.read(buffer.data(), fileSize);
// Process data
std::string content(buffer.begin(), buffer.end());
std::cout << "First 100 characters: " << content.substr(0, 100) << std::endl;
inFile.close();
return 0;
}#File Copy Operation
#include <iostream>
#include <fstream>
int main() {
std::ifstream src("source.txt", std::ios::binary);
std::ofstream dest("destination.txt", std::ios::binary);
if (!src || !dest) {
std::cout << "Error opening files!" << std::endl;
return 1;
}
// Copy file content
dest << src.rdbuf();
src.close();
dest.close();
std::cout << "File copied successfully!" << std::endl;
return 0;
}