Skip to content

Instantly share code, notes, and snippets.

@imryan
Created October 10, 2013 21:51
Show Gist options
  • Select an option

  • Save imryan/6926210 to your computer and use it in GitHub Desktop.

Select an option

Save imryan/6926210 to your computer and use it in GitHub Desktop.
Reading/writing text files.
#include <iostream>
#include <fstream>
#include <string>
void w();
void r();
using namespace std;
int main()
{
w();
r();
return 0;
}
void w()
{
ofstream outf("sample.txt");
if (!outf)
{
cerr << "uh oh, sample.txt couldn't be opened for writing!" << endl;
exit(1);
}
// write two lines into the file
outf << "line 1: swag" << endl;
outf << "line 2: swag" << endl;
}
void r()
{
ifstream inf("sample.txt");
if (!inf)
{
cerr << "uh oh, sample.txt couldn't be opened for reading!" << endl;
exit(1);
}
while (inf)
{
string strinput;
inf >> strinput;
cout << strinput << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment