/***************************************************************
* Name: Prof. Rafael Orta
* Course: Computer Science & Programming
* Class: CS04103 Section: 1 & 2
* Assignment Date: 10/01/20
* File Name: Chapter5.cpp
*****************************************************************
* ID: Chapter 5
* Purpose: Demonstrate how to write to and read from a file
*****************************************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
// Writing to a file
ofstream writeFile;
writeFile.open ("CSNP.txt");
writeFile << "Writing this to a file.\n";
writeFile << "Writing another line to a file.\n";
writeFile.close();
// Reading from a file
ifstream readFile ("CSNP.txt");
if (readFile.is_open())
{
while ( getline (readFile,line) )
{
cout << line << '\n';
}
readFile.close();
}
else cout << "Unable to open file";
return 0;
}
Writing this to a file.
Writing another line to a file.