#include #include #include using namespace std; int main() { // a "stream" is a sequence of data that // you read or write // e.g. cin is an "input stream" // e.g. cout is an "output stream" // e.g. cerr is an "output stream" // stream operators are << and >> // >> reads the next item // << writes the next item cout << "Please tell me your name: "; string name; cin >> name; cout << "Hello, " << name << endl; // create an input file stream // we will use ifstream (requires library) ifstream infile; // try to open the file infile.open( "ints.txt" ); // assumes file is located in the // "current working directory" // (e.g. project folder in Visual Studio) int x; // keep reading integers from the file // until EOF (end-of-file) while ( infile >> x ) { cout << "Read " << x << endl; } infile.close(); // free up oper sys resources cout << "All done reading from file.\n"; // write to a file using ofstream // (requires the library) ofstream outfile; // try to open (create) the file for writing outfile.open( "output.txt" ); // this will OVERWRITE any existing file outfile << "Hello again, " << name << ".\n\n"; outfile << "Last number read from file was " << x << endl; outfile.close(); cout << "All done writing to new file.\n"; return 0; }