c++ - reading a line from ifstream into a string variable -
c++ - reading a line from ifstream into a string variable -
in next code :
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string x = "this c++."; ofstream of("d:/tester.txt"); of << x; of.close(); ifstream read("d:/tester.txt"); read >> x; cout << x << endl ; }
output :
this
since >> operator reads upto first whitespace output. how can extract line string ?
i know form of istream& getline (char* s, streamsize n );
but want store in string variable. how can ?
use std::getline()
<string>
.
istream & getline(istream & is,std::string& str)
so, case be:
std::getline(read,x);
c++ string ifstream getline
Comments
Post a Comment