Jun 1, 2011 at 7:15am UTC
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 Jun 1, 2011 at 7:15am UTC
Jun 1, 2011 at 7:18am UTC
When you use operator>>, it'll just read one word.
If you want a full line, use getline instead:
getline(cin,mystring);
Jun 1, 2011 at 7:22am UTC
Thank you for the quick reply! I thought I had to use something from the sstream class.
Last edited on Jun 1, 2011 at 7:23am UTC