Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Meta-Ridley on September 28, 2005, 07:46 PM

Title: Simple Program
Post by: Meta-Ridley on September 28, 2005, 07:46 PM
It's been quite a loooong time since i've touched C++, years in fact.  And after these many years, I've found a very simple but effective use for it where I work.  However, because it's been so long since I've used C++, I dont remember a thing.  If someone could help build this program, I would be infinately grateful.  The thing the program needs to do:
1. Take .txt files from a floppy
2. Take certain specified characters from the .txt file from which the location on the txt file should never change, and print these characters in another seperate .txt file.
3. Cycle through all .txt files on the floppy

Like I said, simple program.  If someone could help me out with this, I would be very appreciative.

EDIT:  One thing I forgot to mention, if someone would like to help me out, please E-Mail me at [email protected]

I have C++ 6.0 and the compiler, I just don't remember how to write the code anymore.
Title: Re: Simple Program
Post by: Kp on September 28, 2005, 08:14 PM
This is off-the-cuff, so no warranties.  That said:

#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>

static void handle_txt(const char *name) {
  char buf[32];
  int fd = open(name, O_RDONLY), fd2;
  if (fd < 0)
    return;
  fd2 = open("/tmp/other_text_file.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
  if (fd2 < 0) {
    close(fd);
    return;
  }
  lseek(fd, OFFSET1, SEEK_SET);
  read(fd, buf, LENGTH1);
  write(fd2, buf, LENGTH1);
  close(fd);
  close(fd2);
}

int main(int argc, char **argv) {
  DIR *dir = opendir("/mount/floppy");
  struct dirent *de;
  if (!dir)
      return 1;
  chdir("/mount/floppy");
  while ((de = readdir(dir)) != NULL) {
    int l = strlen(de->d_name);
    if (l < 4 || strcmp(de->d_name + l - 4, ".txt"))
      continue;
    handle_txt(de->d_name);
  }
}


It should be pretty obvious how it works.  Tweak it a bit for your offsets and you're good to go.
Title: Re: Simple Program
Post by: MyndFyre on October 03, 2005, 01:05 PM
<3 Kp.  :)
Title: Re: Simple Program
Post by: Mangix on October 05, 2005, 06:14 PM
QuoteC++ 6.0 and the compiler
*Microsoft Visual C++ 6.0
C++ isnt a program or compiler
Title: Re: Simple Program
Post by: Yoni on October 07, 2005, 07:47 PM
Kp:

You get a gold star.