Here is the same example in C++, if you're more comfortable with C++
Code:
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char* argv[])
{
std::string buffer;
bool skip = false;
if(argc != 4)
{
std::cout << "Usage: " << argv[0]
<< " <in_file> <out_file> <delim_word>" << std::endl;
return -1;
}
std::ifstream in_file(argv[1], std::ios::in);
if(!in_file)
{
std::cout << "Error opening input file " << argv[1] << std::endl;
return -1;
}
std::ofstream out_file(argv[2], std::ios::out);
if(!out_file)
{
std::cout << "Error opening output file " << argv[2] << std::endl;
in_file.close();
return -1;
}
while(!in_file.eof())
{
std::getline(in_file, buffer);
if(in_file.fail())
{
std::cout << "Error reading from file " << argv[1] << std::endl;
in_file.close();
out_file.close();
return -1;
}
if(!skip)
if(std::string::npos != buffer.find(argv[3], 0))
skip = true;
if(skip)
{
out_file << buffer << std::endl;
if(out_file.fail())
{
std::cout << "Error writing to file " << argv[2] << std::endl;
in_file.close();
out_file.close();
return -1;
}
}
}
if(!skip)
std::cout << "The word \"" << argv[3] << "\" wasn't found in "
<< argv[1] << std::endl;
in_file.close();
out_file.close();
return 0;
}
Note: None of the above code has been tested, not even compiled.
If you'd like to come up with your own routine, the
eof description gives a very good example.