通过 std::ifstream 从字符串中获取每个值 [英] Get each value from a string via std::ifstream

查看:29
本文介绍了通过 std::ifstream 从字符串中获取每个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 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.

这篇关于通过 std::ifstream 从字符串中获取每个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆