Multiple file input...

From the command line, I want to execute the program with the directory of the files I plan to process as an arguement.

C:\> catch c:\myfiles\

From within the main(), I can set string dir = argv[1], but how would I then open *.txt for processing?

Thanks in advance!
So, basically you are thinking of something like this?

1
2
3
4
5
6
7
8
9
int main() {
     string directory;
     fstream file;
     cout<<"Enter file path: ";
     cin>>directory;
     file.open(directory);
     //...
     file.close(directory);
}


Or am I not understanding you?
Last edited on
I have a project that should process all .txt files within a directory. The path to the files should be stated at the time of execution. So if the path is C:\myfiles\, then the execution of the program from the command prompt should be:

catch.exe C:\myfiles\.

However, I am unaware of how to probe the directory to input all .txt files without specifically stating the file name. I can process individual files from the command prompt such as:

catch.exe C:\myfiles\mytext.txt

But this is not what I need.
Is this what you want?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <dirent.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

static void lookup(const char *arg)
{
    DIR *dirp;
    struct dirent *dp;

    
    if ((dirp = opendir(arg)) == NULL) 
    {
        printf("couldn't open %s", arg);
        return;
    }

    printf("\nText files in %s...\n",arg);
    do 
    {        
        if ((dp = readdir(dirp)) != NULL) 
       {
            char * fName = dp->d_name;
            char * ext   = strrchr(fName,'.');
            
            if (ext == NULL || strcmpi(ext,".txt") != 0)
                continue;

            printf("%s\n", dp->d_name);
        }
    } while (dp != NULL);

    closedir(dirp);    
}


int main(int argc, char *argv[])
{
    int i;
    for (i = 1; i < argc; i++)
        lookup(argv[i]);
    return (0);
}
Topic archived. No new replies allowed.