ofstream and ifstream c++ code example
Example 1: opening file in c++
/ fstream::open / fstream::close
#include <fstream>
int main () {
std::fstream fs;
fs.open ("test.txt", std::fstream::in | std::fstream::out | std::fstream::app);
fs << " more lorem ipsum";
fs.close();
return 0;
}
Example 2: ifstream c++
#include <iostream>
#include <fstream>
int main () {
std::ifstream ifs ("test.txt", std::ifstream::in);
char c = ifs.get();
while (ifs.good()) {
std::cout << c;
c = ifs.get();
}
ifs.close();
return 0;
}
Example 3: write to file in C++
ofstream myfile;
myfile.open("file.txt");
myfile << "write this to file"