C++ provides powerful mechanisms for file input and output through the library.
ifstream - For reading from files.ofstream - For writing to files.ios::in - Read mode.ios::out - Write mode.ios::app - Append mode.#include <iostream>
#include <fstream>
int main() {
std::ofstream outfile("example.txt");
outfile << "Hello, World!";
outfile.close();
return 0;
}
#include <iostream>
#include <fstream>
int main() {
std::ifstream infile("example.txt");
std::string line;
while (std::getline(infile, line)) {
std::cout << line << '\n';
}
infile.close();
return 0;
}
Always remember to close files after use to free system resources!