Thread: command line argument to accept entire directory

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    command line argument to accept entire directory

    I am writing a program called mywc that mimics a scaled back version of wc. It accepts as command line arguments file(s) and tallies the number of characters, words and lines in that file(s). My particular problem is that I want it to accept all files from the current directory if the user enters "mywc *" rather than having them type out all the files by name. I'm sure that it is possible, but I'm not sure how to do it. Thanks.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    Hi There,
    I am not sure of Unix (where wc exists) but i can show you the way in DOS (I guess you can fairly adapt it into Unix).

    DOS's DIR command has a switch "/b" called bare format...

    Once you use it, it displays the contents of the current directory with only file names and no other stuff.
    eg:
    dir c:\dos /b gives you...

    command.com
    abc.a
    def.b

    and so on. ...

    so what you can do is issue a system call like

    system("dir c:\dos /b >> files.txt");

    Once you do this, all the bare file names would get into the file files.txt. Open that file and read line by line for each file name. adn then proceed to do what ever u want to.

    I'm sure "ls" also has some exciting switch like this which could be of some help to you.

    If nothing like this exists then you have to take out the directory structure of UNIX and work.

    Regards,
    Sriharsha
    Help everyone you can

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    15

    Issuing system calls in UNIX

    The mywc program would, ideally, work in both OSs. However, this is not an ideal world and I'm still unfamiliar with what works in one OS and not in another. Thanks to your post, I was able to do a search and find the post below. I haven't been able to try it yet, but will shortly. Thanks, vsriharsha, for your help.


    ************************************************** **

    If you are using a POSIX operating system (UNIX, Linux, etc) you would use opendir() and readdir() to get the directory information. You need to include sys/types.h and dirent.h to use these functions. Here are the function prototypes:

    code:-----------------------------------------------------------------------------
    DIR *opendir(const char *name);
    struct dirent *readdir(DIR *dir);
    ------------------------------------------------------------------------------------

    Pry open the dirent.h header file for the details in the dirent structure (I'm not in front of a *nix system at the moment, and my memory is failing).

    When you are finished with the directory, be sure to call closedir():

    code:-----------------------------------------------------------------------------
    int closedir(DIR *dir);
    ------------------------------------------------------------------------------------

    Hope that helps.

    - Jason Deckard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding files in a directory
    By smarta_982002 in forum C Programming
    Replies: 1
    Last Post: 01-25-2008, 10:10 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Testing if an argument is a directory
    By kahad in forum C Programming
    Replies: 5
    Last Post: 11-08-2006, 04:06 PM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. Compressing an entire directory
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-20-2002, 05:49 AM