View Single Post
Old 09-12-2005, 12:37 PM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote