Thread: reading in command line arguments from a file?

  1. #16
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Most people cop out and write the help file if they have an error with the arguments cause it's much easier.
    Yep,did that first...

    If you don't supply arguments, then you have to prompt the user for the stuff you need,
    did this second...
    your code( while well over my head)compiles fine but crashes the program...Ive much to learn.

    My guess is that the txt file is used as a 'default' set of arguments. *shrug*
    that right...say you need to run/start an app from the registry
    and dont need to enter the args in every time. this was the only way I could think of doing it. is there a better way?

    your code (I can grasp some of it) runs great until the args in the file exceed four...why would this be?

  2. #17
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    >your code( while well over my head)compiles fine but crashes the program
    >your code (I can grasp some of it) runs great until the args in the file exceed four

    Who's code are you referring to?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #18
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    citizen's code...is well over my head
    and I kind of understand yours,bivhitscar...

  4. #19
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    The problem with citizen's code is that he's not allocating memory for the argv array, that's why it crashes.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > The problem with citizen's code is that he's not allocating memory for the argv array, that's why it crashes
    Yes and there are some other things too. The if condition needs to be changed:
    Code:
    if ( (!isspace(ch)) && ch != '\n')
    provided that you add a newline to signal no more arguments. Otherwise the last string is not null terminated and not placed in argv[]

    It's a pity you took my code as is. I warned you I tested nothing.

  6. #21
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    I really appreciate the understanding and patience as I'm learning and very ignorant when it comes to dealing with buffers,pointers,allocating memory...I've only had one very basic c++ class. And we only made it to arrays,every thing else I've learned I've looked for,hunted for,googled for, and asked about...passed my professor up long time ago..so he has not been much help as of late, but he did get me hooked on programming.

    But why cant I exceed four args with your code,bivhitscar. Cause I hate to use something I dont understand...

    citizen, as Im learning and dont fully understand what is going on yet. pity not. I just am not good enough yet to see the genius in what your coding...but Im trying to learn.
    Last edited by g1i7ch; 06-21-2006 at 11:52 PM.

  7. #22
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You should be able to ue more than four arguments with my code, it worked with six for me.

    Can you post your current usage?


    [EDIT]

    Crap, I am trying it with more args now and it's crashing, I'll try to find the error...
    Last edited by bivhitscar; 06-21-2006 at 11:55 PM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  8. #23
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    Im using getopt borrowed from netcat....as well as some netcat source...
    Code:
      
       if ( argc == 1 )//this only allows for 4 options why????
        { 
             if ( (file = fopen("c:\\wnc9.txt", "r")) == NULL )
             {
                  exit(1);
             }
             else
             {
                 if ( fgets( buf, BUFSIZ, file ) != NULL )
                 {
                      position = strtok( buf, " " );
                      
                      arg = malloc( sizeof( arg ) );     
                      *arg = position;                   
                      
                      while ( (position = strtok( NULL, " " )) != NULL )   
                      {
                            argc++;       
                            
                            arg = realloc ( arg, argc );     
                            arg[argc - 1] = malloc( sizeof( arg ) );
                            arg[argc - 1] = position;    
                      }     
                      
                      argv = malloc ( argc );
                      
                      for ( i = 0; i < argc; i++ )
                      {
                          argv[i] = malloc( strlen( arg[i] ) + 1 );
                          strcpy( argv[i], arg[i] );
                      }
                      
                 }
             }
        }
    
       while ((x = getopt (argc, argv, "ade:g:G:hi:lLno:p:rs:tuvw:zHbBP:R:S:c")) != EOF) {
    /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */
        switch (x) {
          case 'a':
    This is how I've been learning, taking apart code. Just now to the point where I can start analyzing one of my favorite tools

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why don't you just use my code? Mine works just fine:
    Code:
    itsme@itsme:~/C$ ./addargs *.c
    argv[0] = ./addargs
    argv[1] = 1kfile.c
    argv[2] = abspath.c
    argv[3] = addargs.c
    argv[4] = afternum.c
    ...
    argv[233] = wrap.c
    argv[234] = zero.c
    argv[235] = -b
    argv[236] = -B
    argv[237] = 999
    argv[238] = -s
    argv[239] = -vv
    argv[240] = -R
    itsme@itsme:~/C$
    It's frustrating when I try to help someone and they totally disregard my statement that another program won't work as-is, and tries it anyway and then discovers (gasp!) it doesn't work. And it's doubly frustrating when I write a working program and someone totally disregards it. You'll be lucky to get help from me again.

    FYI, bv, one of your errors:
    Code:
                            arg[argc - 1] = malloc( sizeof( arg ) );
                            arg[argc - 1] = position;
    You're allocating a pointer's worth of memory and then losing it right on the next line. Did you mean this instead?
    Code:
    arg[argc - 1] = malloc(strlen(position) + 1);
    strcpy(arg[argc - 1], position);
    Last edited by itsme86; 06-22-2006 at 01:16 AM.
    If you understand what you're doing, you're not learning anything.

  10. #25
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Nah, I was thinking about it the wrong way. I only wanted an array of pointers (to buf, in this case) - I got a bit confused.

    And I just wrote out a whole new version, even after reading through yours because I thought it wasn't using argv - again, confusion on my part.

    As it took me so long, I just have to post it:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char **argv)
    {
        FILE * file;
        char buf[BUFSIZ], tmp[BUFSIZ];
        char *position;
        char ch;
        int i, j, offset = 0;
    
        if ( argc == 1 )
        { 
             if ( (file = fopen("args.txt", "r")) == NULL )
             {
                  exit(1);
             }
             else
             {
                 if ( fgets( buf, BUFSIZ, file ) != NULL )
                 {
                      position = strtok( buf, " " );
                      
                      while ( (position = strtok( NULL, " " )) != NULL )   
                      {
                            argc++;
                      }     
                      
                      argv = malloc( argc );
                      
                      for ( i = 0; i < argc; i++ )
                      {
                          argv[i] = malloc( strlen( &buf[offset] ) + 1 );
                          strcpy( argv[i], &buf[offset] );
                          offset += strlen( &buf[offset] ) + 1;
                      }
                      argv[argc] = NULL;
                 }
             }
        }
        
        ch = getchar();
        
        return 0;
    }
    Last edited by bivhitscar; 06-22-2006 at 01:30 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  11. #26
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    argv = malloc( argc );
    That line is still wrong. You're going to have to multiply argc by the size of a pointer to have enough room.

    Also, I think you're going to end up with a '\n' at the end of the last added arg (from the fgets() input).
    If you understand what you're doing, you're not learning anything.

  12. #27
    Registered User
    Join Date
    Jun 2006
    Posts
    28
    itsme86, my sincerest apologies....
    I was bombbarded(not complaining here) with so may replys I lost you by the next page.
    and I had not disregarded your post just have not had time to try it. wuz workin through the post and am still trying to grasp what is going on with the first solution provided...I will try tomorrow

    Thank you all so much for every thing....I have never met so may experienced programmer so willing to help a ignorant newcomer.

  13. #28
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    You're correct itsme, about the newline. And I had a feeling the line you mentioned was wrong too. Should it be argv = malloc ( argc * sizeof( argv ) )?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  14. #29
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    close...just change sizeof( argv ) to sizeof( *argv ). The one you posted will work, but just by chance.
    If you understand what you're doing, you're not learning anything.

  15. #30
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Cool bananas, thanks mate.

    Looking over some of the code I've written makes me realise how much I've actually learnt in the past six months. Definitely helped along by people like youself and salem and co.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM