// writing on a text file #include <iostream> #include <fstream> using namespace std;
int main () { ofstream myfile ("example.txt"); if (myfile.is_open()) { myfile << "This is a line.n"; myfile << "This is another line.n"; myfile.close(); } else cout << "Unable to open file"; return 0; }
读文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// reading a text file #include <iostream> #include <fstream> #include <string> using namespace std;
int main () { string line; ifstream myfile ("example.txt"); if (myfile.is_open()) { while ( getline (myfile,line) ) { cout << line << 'n'; } myfile.close(); } else { cout << "Unable to open file"; } return 0; }
近期评论