【发布时间】:2020-03-10 02:17:26
【问题描述】:
我的代码是这样的:
using namespace std;
string pth("./xxx.pb");
ifstream fin(pth, ios::in | ios::binary | ios::ate);
int len = fin.tellg();
fin.seekg(0);
cout << "compare:\n";
string s1(len, 0);
fin.read(&s1[0], len);
for (auto &el : s1)
cout << (int)el << ", ";
cout << endl;
fin.clear();
fin.seekg(0);
stringstream ss;
fin >> ss.rdbuf();
for (auto &el : ss.str())
cout << (int)el << ", ";
cout << endl;
输出是这样的:
compare:
10, 5, 104, 101, 108, 108, 111, 18, 2, 4, 6, 26, 7, 102, 108, 111, 97, 116, 51, 50, 32, 3,
5, 104, 101, 108, 108, 111, 18, 2, 4, 6, 26, 7, 102, 108, 111, 97, 116, 51, 50, 32, 3,
似乎字符串流在从文件中读取时跳过了第一个字节。我的问题出在哪里?
【问题讨论】:
-
@paddy 我不使用
>>,如何将其添加到我的代码中? -
我不明白。你是写
fin >> ss.rdbuf()的人,对吧?所以它会变成fin >> std::noskipws >> ss.rdbuf() -
对不起,我的错,我已经按照你的建议解决了这个问题。非常感谢。
标签: c++ io fstream stringstream