-2

i have a cpp code

 #include<iostream>
 #include<fstream>
 #include<string>

  using namespace std;
  int main()
 {
    string s,s1;
    ofstream f("new.txt");
     cin>>s;
     f<<s;
    f.close();
    ifstream f1("new.txt");
    while(!f1.eof())
    {
     f1>>s1;
     cout<<s1;
    }
 return 0;

  }

if i give the input string as "MAGICAL STRING WITH SPACES" . The ofstream object only writes "MAGICAL" into the textfile, what do i do to write and read whitespaces from string variables into an output file stream ?

1 Answers1

-2

This is because you use cin. Try getline() instead!

string newArgument = "";
getline(cin, newArgument);
Emanuele
  • 112
  • 2
  • 11
  • 2
    cin can do this. Don't recommend `gets` as it is deemed "unsafe to use". – Jongware Aug 02 '15 at 17:01
  • cin can do this too. the problem isnt with the iobuffers, but the filebuffer objects, not being able to read whitespaces . what do i do ? – Amal Majeed Aug 02 '15 at 17:18