Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- int main(){
- string data;
- int age;
- ofstream outfile;
- outfile.open("data.dat");
- cout << "Writing to the file... " << endl;
- cout << "Enter your name: ";
- getline(cin, data);
- outfile << data << endl;
- cout << "Enter your age: ";
- cin >> age;
- outfile << age << endl;
- outfile.close();
- string fdata;
- int fage;
- ifstream infile;
- infile.open("data.dat");
- cout << "\nReading from the file..." << endl;
- getline(infile, fdata);
- infile >> fage;
- cout << fdata << endl;
- cout << fage << endl;
- infile.close();
- infile.open("ok.dat");
- string contents, line;
- infile.open("ok.dat");
- cout << "\nReading from the file..." << endl;
- while(getline(infile, line)) {
- contents += line + '\n';
- }
- cout << "File Contents:" << endl;
- cout << contents;
- infile.close();
- return 0;
- }
- /* OUTPUT:
- Writing to the file...
- Enter your name: John Doe
- Enter your age: 99
- Reading from the file...
- John Doe
- 99
- Reading from the file...
- File Contents:
- ----- BEGIN ok.txt -----
- Hello World!
- Printing entire contents of the file at once.
- This is being done by using a while loop.
- READ
- WRITE
- OPEN
- CLOSE
- ABCDEFGHIJKLMNOPQRSTUVWXYZ
- 1234567890
- `~!@#$%^&*()_-=+{}[]\|;:"'<,>.?/
- ----- END ok.txt ------
- */
Advertisement
Add Comment
Please, Sign In to add comment