Если вам не нужна промежуточная строка, вы можете скопировать из ifstream
непосредственно в новый ofstream
, используя стандартные алгоритмы:
#include <algorithm>
#include <fstream>
#include <iterator>
#include <string>
struct has_no_plus {
bool operator()(const std::string& str)
{
if (str.find('+') != std::string::npos)
return false;
else
return true;
}
};
int main()
{
std::ifstream ifs("file.txt");
std::ofstream ofs("copy.txt");
std::remove_copy_if(std::istream_iterator<std::string>(ifs),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(ofs, "\n"),
has_no_plus());
// or alternatively, in C++11:
std::copy_if(std::istream_iterator<std::string>(ifs),
std::istream_iterator<std::string>(),
std::ostream_iterator<std::string>(ofs, "\n"),
[](const std::string& str)
{
return str.find('+') != str.npos;
});
}