Skip to main content
added 122 characters in body
Source Link
Benjamin Lindley
  • 104.2k
  • 11
  • 210
  • 285

If you're trying to copy the whole file to a stringstream, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

And then of course, to copy that to a string, like the others are saying:

str = oss.str(); 

If you just want to get a single line, then skip the stringstream, and just use getline:

std::getline(ifs,str); 

If you're trying to copy the whole file to a stringstream, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

And then of course, to copy that to a string, like the others are saying:

str = oss.str(); 

If you're trying to copy the whole file to a stringstream, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

And then of course, to copy that to a string, like the others are saying:

str = oss.str(); 

If you just want to get a single line, then skip the stringstream, and just use getline:

std::getline(ifs,str); 
added 106 characters in body; added 1 characters in body
Source Link
Benjamin Lindley
  • 104.2k
  • 11
  • 210
  • 285

If you're trying to copy the whole file to a stringstringstream, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

And then of course, to copy that to a string, like the others are saying:

str = oss.str(); 

If you're trying to copy the whole file to a string, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

If you're trying to copy the whole file to a stringstream, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf(); 

And then of course, to copy that to a string, like the others are saying:

str = oss.str(); 
Source Link
Benjamin Lindley
  • 104.2k
  • 11
  • 210
  • 285

If you're trying to copy the whole file to a string, then this:

oss << ifs; 

is wrong. All that does is prints the address of ifs. What you want to do is this:

oss << ifs.rdbuf();