-1

I know that I shouldn't use namespace std (using namespace std) as global in this scope, but for this example, I will.

What I ideally want, is to save text to a .txt file and retrieve it. I know that the best option would be fstream and so I decided to use it.

The below code prints out "This". Why does it print out "This" and not the full text?

Here is the code:

#include <iostream> #include <fstream> #include <string> using namespace std; int main() { ifstream fin; ofstream fout; string text; fout.open("C++_text_file_test.txt"); fout << "This is basic text"; fout.close(); fin.open("C++_text_file_test.txt"); fin >> text; fin.close(); cout << text << endl; cin.get(); // waits for user to press enter return 0; } 

I tried researching but didn't quite understand the process of using a loop. Is there an easier way or can someone please explain it?

0

1 Answer 1

0

The >> operator, when used to extract a string, only grabs the characters up to the first white space character. Try using getline to get the whole line.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.