Reading from an iostream

最后都变了- 提交于 2020-01-15 03:34:06

问题


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 anyway 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 >> out;
    //do some stuff with the string
}

This seems to work but out will be equal to "Maybe" rather than the full string.


回答1:


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/




回答2:


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);



回答3:


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.




回答4:


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 (u pto 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 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).



来源:https://stackoverflow.com/questions/7789007/reading-from-an-iostream

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!