Reading from an iostream
Maybe I'm missing something, but I'm having a lot of trouble finding any information on how to how to read from an iostream (std::iostream& stream)
. Is there a way I can convert it to a string or similar?
For clarification this is (what I'm basically trying to do, for example):
std::stringstream ss("Maybe I'm missing something \n but I'm having a lot of trouble finding any information on how to how to read from an iostream.");
readStream(ss);
void readStream(std::iostream& stream)
{
std::string out;
stream >>开发者_StackOverflow out;
// Do some stuff with the string
}
This seems to work, but out
will be equal to "Maybe" rather than the full string.
You read from an iostream the same way you would if you were using cin.
stream >> varName;
Crazy syntax yes, but that's what the makers of streams decided to do.
You can also use get and getline if your reading to strings. Get will get the next character or a specified buffer of characters, and getline will go to the next newline.
getline(stringName);
You can read more on this here: http://cplusplus.com/reference/iostream/iostream/
Streams converts automatically for the type they are shifting to.
using namespace std;
int number;
double fraction;
string world;
stream >> number >> fraction >> world;
When shifting to a string, it reads until the first word delimiter, you may wish to use std::getline.
using namespace std;
string line;
getline(stream,line);
Maybe you want to read whole lines. In this case you have to use std::getline
, thus having:
void readStream(std::iostream& stream)
{
std::string out;
// while getting lines
while(std::getline(stream, out))
{
// Do some stuff with each line
}
}
You can also choose a line delimiter character, by passing it to std::getline
as a third parameter.
The stream operator >> is used to read formatted white space separated text.
int val1;
stream >> val1; // reads a space separated int
float val2;
stream >> val2; // reads a space separated float
std::string val3;
stream >> val3; // reads a space separated word.
Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (up to the null terminator, '\0', of the C-string).
If you want to read a whole line of text use std::getline()
std::string line;
std::getline(stream, line);
But like most languages, you can loop reading the stream until it is finished.
std::string word;
while(stream >> word)
{
// Reads one word at a time until the EOF.
std::cout << "Got a word (" << word << ")\n";
}
Or the same thing one line at a time:
std::string line;
while(std::getline(stream, line))
{
// Reads one word at a time until the EOF.
std::cout << "Got a word (" << word << ")\n";
}
Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file, but ignore new line.
Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).
精彩评论