Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream> //required to be able to used streams.
- #include <iostream>
- #include <cstdlib> //require for the exit function
- using namespace std;
- int main (){
- string str1,str2; //variables for input of user
- //declaring streams
- ifstream in_stream; //stream for input
- ofstream out_stream; //stream for ouput
- //prompt user for string input
- cout << "Please enter first word or sentence: " << endl;
- getline( cin, str1, '\n'); //reads a full line instead of just one word... '\n' = new line.
- cout << endl; //extra spacing
- cout << "Please enter second word or sentence: " << endl;
- getline( cin, str2, '\n');
- cout << endl;
- out_stream.open("engg233.txt"); //open the file to read from it.
- out_stream << str1 << endl << str2;
- out_stream.close(); //close the file.
- in_stream.open("engg233.txt"); //Connect the file... then/or Create/Replace the file.
- //checks for proper connection
- if(in_stream.fail())
- {
- cout << "Input file does not exist, please try again." << endl;
- exit(1); //exit the program.
- }
- getline(in_stream, str1, '\n');
- getline(in_stream, str2, '\n');
- in_stream.close();//close the file
- cout << str1 << endl << str2;
- //print String in"sdfasdf"serting in the 'str1' and 'str2' that were then stores inside 'in_stream
- return 0;
- }
Add Comment
Please, Sign In to add comment