// Listing 14.01 Opening Files for Read and Write
#include <fstream> // ifstream, ofstream
#include <cassert> // assert()
#include <iostream>
using namespace std;
int main() {
char file_name[80] = "";
char buffer[255] = ""; // for user input
cout << "File name: ";
cin >> file_name;
ofstream fout(file_name); // establish connection,
assert( fout.is_open() ); // and check for success
//fout << "This line written directly to the file...\n";
cout << "Enter text for the file: ";
cin.ignore(1,'\n'); // eat the newline after the file name
cin.getline(buffer,255,'\n'); // get the user's input
fout << buffer << "\n"; // and write it to the file
fout.close(); // close the connection, ready for reopen
// PARTE 2
ifstream fin(file_name); // establish connection,
assert( fin.is_open() ); // and check for success
cout << "\n\nHere's the contents of the file:\n\n";
char ch;
while (fin.get(ch))
cout << ch;
cout << "\n***End of file contents.***\n";
fin.close(); // close the connection
return 0;
}