【发布时间】:2012-07-02 01:18:10
【问题描述】:
我需要使用字符串流从文件中读取表达式并将表达式转换为另一种形式。但我无法弄清楚如何使用 Istringstream 从文件中读取行。任何人都可以帮助我解决#includes 和语法吗?谢谢
【问题讨论】:
-
你不能,你需要一个 file 流来读取文件,string 流用于字符串。
标签: c++ stream istringstream
我需要使用字符串流从文件中读取表达式并将表达式转换为另一种形式。但我无法弄清楚如何使用 Istringstream 从文件中读取行。任何人都可以帮助我解决#includes 和语法吗?谢谢
【问题讨论】:
标签: c++ stream istringstream
#include <fstream>
std::ifstream file("filename.txt");
StuffType stuff;
while(file >> stuff)
{
// If you are here you have successfully read stuff.
}
【讨论】:
作为对上面 Dave 回答的补充:要从文件中读取一行,您可以使用以下代码:
char buf[256];
file.getline(buf,256);
字符串 buf 然后包含文件中的文本行。
【讨论】:
getline for string 不是会员。语法为std::getline( file, buf );