Handling multiple input files from command line

This is a discussion on Handling multiple input files from command line within the C Programming forums, part of the General Programming Boards category; Hi all, I need some advise on how to handle multiple possibilities in the way of files from the command ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Handling multiple input files from command line

    Hi all,

    I need some advise on how to handle multiple possibilities in the way of files from the command line. I'm not asking how to use command-line arguments, but how best to handle possible parameters.

    For example, I have a command-line tool that will accept the following file possibilities:

    singleFile.fm
    *.fm
    ---------------
    single.bk
    single.book
    single.fmb
    *.bk
    *.book
    *.fmb
    ---------------
    *.*

    I'm looking for some advice/ideas on how to handle such a wide range of file input possibilities. As you can see, it can be a single file, all files of a particular extension, or all files within the directory.

    Your thoughts appreciated!

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You know that shell does wildcard expansion? ("cmd /?" or "man bash") If you want to do it yourself, you can define your own wildcards/patterns as you like because you're gonna have to scan for every file/dir and do matching yourself.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    26
    man scandir

    Code:
    /* print files in current directory in reverse order */
    #include <dirent.h>
    main(){
        struct dirent **namelist;
        int n;
        n = scandir(".", &namelist, 0, alphasort);
        if (n < 0)
            perror("scandir");
        else {
            while(n--) {
                printf("&#37;s\n", namelist[n]->d_name);
                free(namelist[n]);
            }
            free(namelist);
        }
    }
    (Not in POSIX.1-2001, but alphasort() and scandir() are under consideration for a future revision to POSIX.1. )

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Sorry, I should have mentioned that the app I'm writing is for Windoze machines.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ok. So did you read any of rasta's response? This is still going to involve talking to the operating system and doing some scanning.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    When you type: "myprog.exe a*", shell (cmd.exe in your case) will search for all files in current directory and replace "a*" with all files starting with "a" and call your program like this "myprog.exe a1 a2 a3...". Your prog never sees any "*" or "?". Now, if you want to use "*" or "?" yourself, (or any other character as special/wildcard), first you have to ESCAPE those characters from shell (with \ or " i think), so that shell doesn't do any expansion (or maybe there's a switch/option for cmd.exe to disable it?) and so that your program actually receives "*" and "?", then scan for every file/dir in current directory and do matching against your patterns (you defined yourself or you can copy standard behavior).

    EDIT: of course, if you start myprog.exe from another program (not from shell), there is no shell and no shell expansion, so you can safely pass "*" and "?" along (and do the searching/matching manually...)
    Last edited by rasta_freak; 08-24-2008 at 02:11 AM.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks Mole and Rasta. I appreciate your responses.

    Thanks especially to you Rasta. I didn't know the shell would take care of the expansion for you. I read in a number of places on the internet that the application was responsible for handling wildcard expansion on Windoze machines. I was thinking I would have to deal w/ all possible nuances myself. Off to test this out. . .

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    After a quick test, it looks like the cmd shell in Windoze does not expand wildcards for you. What the program gets is exactly what you type in.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by cnfwriter View Post
    After a quick test, it looks like the cmd shell in Windoze does not expand wildcards for you. What the program gets is exactly what you type in.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[]) {
        printf("Argc is: %d\n", argc);
        printf("Argv are: ");
        for (int i = 0; i < argc; i++) {
            printf("%s ", argv[i]);
            if (!((i+1)%5)) {
                printf("\n");
            }
        }
        return 0;
    }
    Code:
    C:\DOCUME~1\Andrew\Desktop>temp c*
    Argc is: 8
    Argv are: temp casting Chapter04_Code Chapter09_Code check1.txt
    check2.txt CIS460_Syllabus[1].doc crayon
    You do have to (gasp!) type the command yourself, though.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Actually, both of you are right - gcc compiled code parses the argument and expands it before main is called. Compiling with Visual studio, and the argument is passed "as is" to the application. I made a program very similar to tabstops, compiled it with gcc and it printed all the files I expected - so I didn't make the comment that cnfwriter made which I had first intended to make. I then read the two new replies, and realized that "Ah, I think I know where there's a difference here".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks tabstop and matsp.

    Yes, my test program is similar to yours Tabstop, but doesn't yield the same results. Thanks matsp for clearing things up. You are right. I am compiling w/ Visual Studio.

    Is gcc adding some kind of wildcard expansion routines behind the scenes whenever you compile? It could be worth compiling things in gcc just for the benefit of not having to write a bunch of routines to deal w/ wildcard expansion.

    I read that there are already some libraries out there for VS to do this, but I haven't tripped across any yet.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I hadn't realized there would be a difference. A little bit of searching gives this Microsoft page, and who knows whether that helps.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    I hadn't realized there would be a difference. A little bit of searching gives this Microsoft page, and who knows whether that helps.
    It does indeed solve the problem - I just tried it out, and now gcc and cl generated executable does the same thing (that is, lists all the .c files in my temp directory).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    I had to wait until I got into the office this morning to try it out in Visual Studio, but the solution does indeed work as mats mentions.

    Thank you very much for finding this solution Tabstop!! And thanks to you again mats for your insightful help as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  2. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  3. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  4. Need help with input streams from multiple source files
    By orikon in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2005, 02:56 PM
  5. How to input multiple groups of data
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-14-2002, 09:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21