Thread: array of strings?

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    array of strings?

    Hi there,

    I'm trying to pass in an array of strings to a function and am obviously having trouble with the basic idea behind storing strings to an array of strings.

    I have a prototype something like this:

    Code:
    char **read_files(const char *files[], size_t nfiles, size_t *nlines)
    That takes an array of file names, opens them, reads them all ,and stores them to a dynamically allocated array. But right now I can't even pass in the array of filenames.

    In my main, I'm trying to just parse the input file names from the command line, store them to my files[] array and handle them inside the above function.

    So right now I can parse everything and get a single filename, but need to get it into an array.

    Code:
    int main(int argc, char *argv[]) {
    
        FILE *fp;
        int option = 0;
        char *filename;
        size_t nlines;
        size_t nfiles;
        int j;
    
    /* omiting code for parsing options, that is working */
      
        for(j = i; j < argc; j++)
        {
            filename = argv[j];                
            nfiles++;
            printf("%s", filename[j]);
           
       }
    
        read_files(files,  nfiles, size_t *nlines);
        return 0;
    }
    I'm just wondering what the best approach would be to store the filename to an array of filenames and pass them to the function?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    argv is your array of filenames. (Modulo the extra arguments at the beginning, which is where that i is coming from I guess.) So you would probably pass argv+i, whatever that i is, and there you go.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Sorry, I guess I should have just posted the whole thing. Oh, and I should mention this is a homework assignment.

    What I'm doing with i is looping through the command line first to get the options.

    Then when I have an option (only need one), I'm breaking then looping through whatever is next to get the file names.

    Code:
        
    
        int option = 0;
        char *filename;    
        size_t nfiles;
        size_t nlines = 0;
        int i;
        int j;
        for(i = 1; i < argc; i++)
        {
            if(strcmp(argv[i],"-c") == 0)
            {
                option = 1;
                printf("-c: on\n");
            }    
            else if(strcmp(argv[i], "-w") == 0)
            {
                option = 2;
                printf("-w: on\n");
            }    
            else if(strcmp(argv[i],"-r") == 0)
            {
                option = 3;
                printf("-r: on\n");
            }    
            else if(strcmp(argv[i], "--")== 0)
            {
                    i++;
                    break;
            }
            else
                break;       
        }   
        for(j = i; j < argc; j++)
        {
            int index;
            filename = argv[j];
            nfiles++;
                 
       }
        return 0;
    }
    But right now it appears to be storing only the last filename (there can be multiple input files). Or maybe I'm just misunderstanding and it is storing all the file names?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So that's exactly what I thought you had. argv already is your array of filenames (although you will need to skip past the arguments at the top). So just pass argv (actually argv+i for skipping reasons as mentioned above) to your function already and be done with it.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    Thank you for your help tabstop. That makes perfect sense that I've already done the work with argv to store to an array of strings. Sorry, I am definitely a novice at this stuff. I have some tools, but seem to be having trouble putting them together.

    I'm just not sure HOW I pass argv into the function.

    if I have the prototype in my

    Code:
    char **read_files(const char *files[], size_t nfiles, size_t *nlines)
    and I want it to read multiple files, open them and process them in my separate function, wouldn't I need to store each filename (so argv[j], argv[j +1] etc) into another array, then pass that in?

    In main

    p = read_files(????, nfiles, nlines);

    I can't just put
    p = read_files(argv[j], nfiles, nlines); into my loop can I?

    or if I use filename = argv[j]: then try to pass that in, I can't use that either.(I tried and I get an incompatible pointer type warning).

    Sorry, I realize that this will be one of those duh moments for me, but I'm just not sure how to do this.

    ( I haven't fleshed out my read files function yet, just trying to see if I can pass in values first.)

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    pass "argv+i" just like that into your function. Not argv[i], because that's not an array, that's one slot in your array. Just type "argv+i".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM