【发布时间】:2014-04-24 16:26:11
【问题描述】:
我有一个包含混合数据的自定义文件。在文件的末尾有一个我想要检索的完整图像。
问题是,当我将其“提取”并粘贴到图像文件中时,rdbuf() 会留下一些烦人的 CR LF 字符,而不仅仅是原始的 LF 字符。
我已经以二进制模式打开了两个流。
using namespace std;
ifstream i(f, ios::in | ios::binary);
bool found = false; // Found image name
string s; // String to read to
string n = ""; // Image name to retrieve
while (!found) {
getline(i, s);
// Check if it's the name line
if (s[0]=='-' && s[1]=='|' && s[2]=='-') {
found = true;
// Loop through name X: -|-XXXX-|-
// 0123456789
// Length: 10 3 6
for (unsigned int j=3; j<s.length()-4; j++)
n = n + s[j];
}
}
ofstream o(n.c_str(), ios::out | ios::binary);
o << i.rdbuf();
【问题讨论】:
-
这不能回答问题,但
for循环中的条件应该是j < s.length()-3或j <= s.length()-4 -
我知道,一开始是这样的,但不知什么原因,在测试时我发现它占用了一个额外的字符,所以我将它设置为-4。仍然不知道为什么会这样._。顺便说一句,感谢您的阅读并愿意提供帮助^^
-
已测试问题:似乎 getline 将“换行符”字符添加到字符串中。这就是为什么它需要去 s.length()-4