通过 std::ifstream 从字符串中获取每个值

2022-01-04 00:00:00 get c++ ifstream std

我尝试在 while 循环中使用 ifstream 来获取每个值.但是,当我尝试时,没有任何反应.为什么?

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

推荐答案

您必须使用 istringstream,而不是 ifstream.

You must use an istringstream, not an ifstream.

相关文章