问题内容:fstream怎么一次把整个文本文件内容读到一个string里?
- 原讨论链接:http://community.csdn.net/expert/topicview1.asp?id=660148
- 所属论坛:C语言 审核组:C/C++
- 提问者:pi1ot 解决者:babysloth
- 感谢:babysloth、tohigh
- 关键字:函数 参数 控件 数据库 手谈 白子 字段 触发器 C/C++ 工具平台和程序库 C++标准库 变量 语句
- 答案:
infile >> str 碰到回车空格就不继续了。
不知道 std::noskipws 是不是起这个作用的,就算是也不能用了,所使用的 gcc 2.95.2 不支持 noskipws.
------------------------------------------------------------------------
IOStream著名专家Dietmar Kuehl给过两个方法
std::ifstream in("some.file");
std::istreambuf_iterator<char> beg(in), end;
std::string str(beg, end);
或
std::ifstream in("some.file");
std::ostringstream tmp;
tmp << in.rdbuf();
std::string str = tmp.str();