/* fileStuff.cpp Dave Klick CIS 150 2017-03-21 Demonstration of file techniques. */ #include #include #include using namespace std; int main() { int num; string fname("march21.txt"); // Write to a text file (erase current file contents) ofstream outfile(fname.c_str()); // Check for error if (!outfile) { cout << "Error: File " << fname << " failed to open\n"; return EXIT_FAILURE; } outfile << 7 << '\n'; outfile << 5 << '\n'; outfile << 3 << '\n'; outfile << 22 << '\n'; outfile << 35 << '\n'; outfile << 16 << '\n'; outfile << 4 << '\n'; outfile << 99 << '\n'; outfile << 53 << '\n'; outfile << 70 << '\n'; outfile << -2 << '\n'; outfile.close(); // Write to a text file (append to current file contents) ofstream outfile2(fname.c_str(), ios::app); // Check for error if (!outfile2) { cout << "Error: File " << fname << " failed to open\n"; return EXIT_FAILURE; } outfile2 << 42 << '\n'; outfile2.close(); // Read from a text file ifstream infile(fname.c_str()); // Check for error if (!infile) { cout << "Error: File " << fname << " failed to open\n"; return EXIT_FAILURE; } // Read until a read fails to work while (infile >> num) { cout << "Read in " << num << '\n'; } infile.close(); return EXIT_SUCCESS; }