1// reading a text file
2#include <iostream>
3#include <fstream>
4#include <string>
5using namespace std;
6
7int main () {
8 string line;
9 ifstream myfile ("example.txt");
10 if (myfile.is_open())
11 {
12 while ( getline (myfile,line) )
13 {
14 //use line here
15 }
16 myfile.close();
17 }
18
19 else cout << "Unable to open file";
20
21 return 0;
22}
1#include <iostream>
2#include <fstream>
3using namespace std;
4
5int main() {
6 // Create and open a text file
7 ofstream MyFile("filename.txt");
8
9 // Write to the file
10 MyFile << "Files can be tricky, but it is fun enough!";
11
12 // Close the file
13 MyFile.close();
14
1// using ofstream constructors.
2#include <iostream>
3#include <fstream>
4
5std::ofstream outfile ("test.txt");
6
7outfile << "my text here!" << std::endl;
8
9outfile.close();
10