Created
October 10, 2013 21:51
-
-
Save imryan/6926210 to your computer and use it in GitHub Desktop.
Reading/writing text files.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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