Get each value from a string via std::ifstream

2019-03-03 14:29发布

问题:

I am try to use an ifstream with the while loop to get each value. However, when I try it, nothing happens. Why?

std::string line;
std::getline(cin, line);
std::ifstream stream(line);
while(stream){
    std::cout << stream.get();
}

回答1:

You must use an istringstream, not an ifstream.