////////////////////////////////////////////////// // files.cpp - How do I write and read files? // ////////////////////////////////////////////////// #include #include #include using namespace std; void main() { ofstream out("test.dat"); if(!out.good()) { cout << "\n can't open file test.dat for writing\n\n"; return; } else { cout << "\n writing to file test.dat\n\n"; out << "first line\n"; out << "second line\n"; out << "last line\n"; } out.close(); ifstream in("test.dat"); if(!in.good()) { cout << "\n can't open file test.dat for reading\n\n"; return; } cout << "\n reading from file test.dat\n\n"; string line; while(in.good()) { getline(in,line); cout << " " << line << endl; } cout << endl; }