Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-12-2005, 10:29 AM   #1 (permalink)
mjones
Registered User
 
mjones's Avatar
 
Join Date: Sep 2005
Posts: 2
mjones is on a distinguished road
how do I do this

I need to come up with some way to make a program (thing) that will look at a text file modify it and save the modified text file to another place.
The text file is on a network drive and is run every hour.

I need to have the program read the text file and delete everything until it reads a word (the heading of part of the report). Then have it save the remaining report to a text file on the desktop of the computer that ran it.

Our programmer just quit and the only language I can write in (sort of) is C++. Any help would be appreciated.
mjones is offline   Reply With Quote
Old 09-12-2005, 11:11 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Something like this in C
Code:
#include <stdio.h>
#include <string.h>
#define BUFF_LENGTH 256

int main(int argc, char* argv[])
{
  FILE* in_file, out_file;
  char buffer[BUFF_LENGTH +1];
  int skip = 0;
  if(argc != 4)
    {
      printf("Usage: %s <in_file> <out_file> <delim_word>\n", argv[0]);
      return -1;
    }
  if(!(in_file = fopen(argv[1], "r")))
    {
      printf("Error opening input file %s\n", argv[1]);
      return -1;
    }
  if(!(out_file = fopen(argv[2], "w")))
    {
      printf("Error opening output file %s\n", argv[2]);
      fclose(in_file);
      return -1;
    }
  while(!feof(in_file))
    {
      if(!fgets(buffer, BUFF_LENGTH, in_file))
	{
	  printf("Error reading from file %s\n", argv[1]);
	  fclose(in_file);
	  fclose(out_file);
	  return -1;
	}
      if(!skip)
	if(strstr(buffer, argv[3]))
	  skip = 1;
      if(skip)
	if(!fwrite(buffer, strlen(buffer), 1, out_file))
	  {
	    printf("Error writing to file %s\n", argv[2]);
	    fclose(in_file);
	    fclose(out_file);
	    return -1;
	  }
    }
  if(!skip)
    printf("The word \"%s\" wasn't found in file %s\n", argv[3], argv[1]);
  fclose(in_file);
  fclose(out_file);
  return 0;
}
Note: None of the above code has been tested, not even compiled, but it should work
__________________
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
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
Old 09-12-2005, 02:48 PM   #4 (permalink)
mjones
Registered User
 
mjones's Avatar
 
Join Date: Sep 2005
Posts: 2
mjones is on a distinguished road
Sweet, thanks red, the c++ looks way more familiar.
mjones is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 12:05 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting