Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <limits>
- using namespace std;
- void writeFile(const string& filename) {
- ofstream os(filename);
- if (!os) {
- cout << "Error opening the file for writing." << endl;
- return;
- }
- cout << "Enter a line of text: ";
- string input;
- getline(cin, input);
- os << input << endl;
- cout << "Enter a word: ";
- cin >> input;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- os << input << endl;
- os.close();
- }
- void readFile(const string& filename) {
- ifstream is(filename);
- if (!is) {
- cout << "Error opening the file for reading." << endl;
- return;
- }
- string line;
- while (getline(is, line)) {
- cout << line << endl;
- }
- is.close();
- }
- int main() {
- string filename = "Testout.txt";
- writeFile(filename);
- readFile(filename);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement