Please send questions to
st10@humboldt.edu .
/*-----
Contract: main: void -> int
Purpose: provides an example comparing opening a file for
writing, as compared to opening a file for appending;
ask the users for an integer and 2 file names, one to
which to write the integer and one to which to append
the integer.
Examples: Assume that file1.txt in the current working directory
contains:
I am file1.txt.
...and that file2.txt in the current working directory
contains:
I am file2.txt.
Then, if the user now runs the program two_writes, if he/she
responds when prompted with:
1326
file1.txt
file2.txt
...then after this program is done file1.txt contains:
1326
and file2.txt contains:
I am file2.txt.
1326
by: Sharon M. Tuttle
last modified: 4-30-07
-----*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// the open method for an ofstream requires old-style
// C-strings for the file name
const int MAX_FILENAME = 256;
char write_file_name[MAX_FILENAME];
char append_file_name[MAX_FILENAME];
int desired_value;
ofstream write_fstream;
ofstream append_fstream;
cout << "What integer would you like to write to these file? " << endl;
cin >> desired_value;
cout << endl
<< "To what file should this integer be WRITTEN? " << endl;
cin >> write_file_name;
cout << endl
<< "To what file should this integer be APPENDED? " << endl;
cin >> append_file_name;
write_fstream.open(write_file_name);
append_fstream.open(append_file_name, ios::app);
write_fstream << desired_value << endl;
append_fstream << desired_value << endl;
write_fstream.close();
append_fstream.close();
return EXIT_SUCCESS;
}