fstream - string input to file

I'm having a problem outputting a whole string into a text file using fstream. The problem is it only reads the first word of the string entered and negates the rest of the string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(){
    
    string mystring;
    cin >> mystring;
    
    ofstream myfile;
    myfile.open("Example.txt");
    myfile << mystring;
    myfile.close();
    return 0;
}
Last edited on
When you use operator>>, it'll just read one word.
If you want a full line, use getline instead:
getline(cin,mystring);
Thank you for the quick reply! I thought I had to use something from the sstream class.
Last edited on
Topic archived. No new replies allowed.