【问题标题】:How to use IStringStream to read from a file?如何使用 IStringStream 从文件中读取?
【发布时间】:2012-07-02 01:18:10
【问题描述】:

我需要使用字符串流从文件中读取表达式并将表达式转换为另一种形式。但我无法弄清楚如何使用 Istringstream 从文件中读取行。任何人都可以帮助我解决#includes 和语法吗?谢谢

【问题讨论】:

  • 你不能,你需要一个 file 流来读取文件,string 流用于字符串。

标签: c++ stream istringstream


【解决方案1】:
#include <fstream>

std::ifstream file("filename.txt");

StuffType stuff;
while(file >> stuff)
{
    // If you are here you have successfully read stuff.
}

【讨论】:

    【解决方案2】:

    作为对上面 Dave 回答的补充:要从文件中读取一行,您可以使用以下代码:

    char buf[256];
    file.getline(buf,256);
    

    字符串 buf 然后包含文件中的文本行。

    【讨论】:

    • 我也试过了,但找不到正确的语法。也许你能帮忙?这至少不起作用: std::string buf;文件.getline(buf, 256);如果您删除 getline 中的第二个参数,它也不起作用。
    • getline for string 不是会员。语法为std::getline( file, buf );
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多