我仍在练习C ++,并且在文件流上使用char迭代器时遇到问题。
我想将文件的一部分复制到另一个(临时)文件中。我想在第一个文件中找到一个特定的字符串(我使用了std :: find算法),以便知道在哪里可以“剪切”要复制的文件部分(希望如此)。我的问题是,使用以下代码,我遇到了一个我不真正理解的编译错误。
我所讨论的代码部分如下所示:
ifstream readStream(fileName.c_str());
istreambuf_iterator<char> fStart(readStream);
istreambuf_iterator<char> fEnd;
auto position = find(fStart, fEnd, refToReplace); // refToReplace is a std::string
if (position != fEnd){ // If the reference appears in the file
ofstream tempStream("temp.fsr"); // create the temp file
copy(fStart, position , ostreambuf_iterator<char>(fluxTemp)); // and copy the part I want (before the matching string)
}
else{
continue;
}
我在“ stl_algo.h”中遇到了编译错误:
error: no match for 'operator==' in '__first.std::istreambuf_iterator<_CharT, _Traits>::operator*<char, std::char_traits<char> >() == __val'
先感谢您。
编译错误应带有实例化回溯,该回溯告诉您最终导致该错误的调用。
在您的情况下,这将指向find
呼叫。find
查找单个元素,并且迭代器的元素类型为单个字符,但是您传递了一个字符串。(根据您的描述。您的代码段实际上并未告诉我们的类型refToReplace
。)
您要寻找的算法是search
,但是它需要正向迭代器,而流缓冲迭代器则不需要。
您将需要选择其他方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句