Thread: Some direction please

  1. #1
    Registered User
    Join Date
    Jul 2008

    Some direction please

    I need to create a program that will perform a diff on all files in two different directories. Basically it's going to check for any changes between these 2 directories.

    The main "base standard" directory will change as will the comparison directory.

    I read the man on ftw and I believe that is the way I want to go for traversing the directory. However, I need to use the "diff" command for this processing so I need to perform an exec(X) of some sort.

    Here's some sample code I tried and it works well for one file at a time:

    #include <stdio.h>
    main() {
      FILE *fpipe;
      char *command="diff /base/standard.txt /comp/standard.txt";
      char line[256];
      if (!(fpipe = (FILE*)popen(command, "r"))) {
        perror("Problem with pipe");
      while (fget(line, sizeof line, fpipe)) {
        printf("%s", line);
    My question is this. Should I use the popen for each file and store all the output from the diff command in a variable? In which case, I guess I'll have to pclose each time as well. Or should I be looking at pipe/fork/exec? At times there will be directory trees that go 5-6 levels deep and could be hundreds of files. What is the best way to make sure this scales up well?

    Will sending the output of each diff command to a file incur too much time delay? This process needs to be extremely fast.

    Thank you in advance for your guidance.

  2. #2
    Join Date
    Dec 2007
    Why not just use a shell script?

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    If you need fast thing, never do multiprocessing.

    new proccess == new resource == more load time == slow

    capturing another proccess output == ++slow

    I suggest you to make your diff routine, then you may make multithreading program to gain more speed.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    diff -r perhaps?

    rags_to_riches is right, this is prime shell-script territory.

    You could spend weeks writing and debugging C code which will only be 10&#37; quicker than the 5 minute 1-liner you could have typed in at the shell.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    On me hyperplane
    Or, if you have to use C you could use tmpfile Copy the content across, and just scan once in a while. Its not going to be fast, but it would be pretty easy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mouse Maze Problem
    By Furbiesandbeans in forum C++ Programming
    Replies: 13
    Last Post: 04-28-2008, 04:20 PM
  2. getting direction
    By pode in forum Game Programming
    Replies: 5
    Last Post: 10-18-2003, 04:22 PM
  3. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM
  4. direction in directx
    By Unregistered in forum Game Programming
    Replies: 1
    Last Post: 12-24-2001, 08:03 AM
  5. Classic problem doesn't accept direction
    By cheesehead in forum C++ Programming
    Replies: 5
    Last Post: 11-13-2001, 06:32 PM