using arguments in main

This is a discussion on using arguments in main within the C Programming forums, part of the General Programming Boards category; Hello, What I'm trying to do is call a program with 3 parameters. eg. myProgram ABCD input.txt output.txt then I ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

    using arguments in main

    Hello,

    What I'm trying to do is call a program with 3 parameters.
    eg. myProgram ABCD input.txt output.txt

    then I want to add to ABCD argument the extension .test

    As you can see in my code after the functionCall the argv[2] changes value. I believe that the strcat(filename, ".test") causes the problem as the argv[2] takes the value "est" but why is this happening and how can I correct it?

    Thanks
    /m

    Code:
    #include <stdio.h>
    #include <strings.h>
    
    
    void functionCall (char* filename) {
    
    
        strcat(filename, ".test");
        FILE *file = fopen(filename, "r");
    
    
        if ( file != NULL ){
            //code
        }
    }
    
    
    int main (int argc, char * argv[]){
    
    
       int i;
    
    
        //DEBUG: PARAMETERS BEFORE
        printf("\n Program %s called with %d parameters:\n", argv[0], argc-1);
        for (i = 1; i < argc; i++)
          printf("\t%s \n", argv[i]);
    
    
        functionCall(argv[1]);
    
    
        //DEBUG: PARAMETERS AFTER
        printf("\n Program %s called with %d parameters:\n", argv[0], argc-1);
        for (i = 1; i < argc; i++)
        printf("\t%s \n", argv[i]);
    
    
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    You're running into a buffer overflow when you strcat something to the values pointed by argv. The program arguments are guaranteed only to have enough memory to hold each of the arguments passed to the program, and no more.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    Thanks now I understand Why this is happening, any suggestions to fix it? I tried to concatenate in another function but since it's a pointer nothing changes. Is there a way to assign the argv[1] value (the one i want to alter by adding the extension) to a char[] (not pointer)?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    It's not a good idea to alter the arguments. Copy the argv[n] into some other char array and concatenate there.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    OK. Always you have to try before you ask I used malloc and it works.

    Thanks once more King Mir.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by nonoob View Post
    It's not a good idea to alter the arguments.
    I don't see any good reason for that, as long as you're careful not to overflow.

    If marlaFC resolved this it how I think, then there's a memory leak, but under the circumstances an acceptable one.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Quote Originally Posted by King Mir View Post
    I don't see any good reason for that, as long as you're careful not to overflow.
    In fact, the C standard specifically allows that. An implementation is required to allow the strings pointed to by the argv array to be modifiable by the program.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    Quote Originally Posted by King Mir View Post
    If marlaFC resolved this it how I think, then there's a memory leak, but under the circumstances an acceptable one.
    This is what I did. So I still have a memory leak (though I don't understand it) but it's an acceptable one? Any other better ideas?

    This is what I did and it works now. So are you saying that I still have a memory leak? Any other better ideas or it's fine like this?

    Code:
    #include <stdio.h> #include <strings.h> void functionCall (char* filename) { char *dst; char ext[] = ".test"; dst = (char *)malloc(strlen(filename)+st​rlen(ext));
    strcpy(dst, filename); strcat(dst, ext); FILE *file = fopen(dst, "r");
    if ( file != NULL ){ //code } } int main (int argc, char * argv[]){ int i; //DEBUG: PARAMETERS BEFORE printf("\n Program %s called with %d parameters:\n", argv[0], argc-1); for (i = 1; i < argc; i++) printf("\t%s \n", argv[i]); functionCall(argv[1]); //DEBUG: PARAMETERS AFTER printf("\n Program %s called with %d parameters:\n", argv[0], argc-1); for (i = 1; i < argc; i++) printf("\t%s \n", argv[i]); return 0; }

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    1) you need to malloc space for the '\0'.

    2) you should free what you allocate.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    Implemented! Thanks!

  11. #11
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Theres a couple (really pedantic) corrections I have, take them as you want:

    You don't need to typecast malloc(), as it returns a void pointer which can be used for any type:

    Code:
    char *dst;
    dst = malloc(strlen(filename + strlen(ext));
    Also:

    Code:
    if (file != NULL) {} /* is the same as : */
    
    if (file) {}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arguments to main
    By cantinero74 in forum C Programming
    Replies: 4
    Last Post: 05-27-2010, 10:39 PM
  2. int main arguments
    By cerin in forum C++ Programming
    Replies: 11
    Last Post: 02-21-2005, 12:20 PM
  3. main() with arguments?
    By barim in forum C Programming
    Replies: 3
    Last Post: 06-25-2004, 02:58 AM
  4. main arguments
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 03-27-2003, 02:56 PM
  5. Main() arguments
    By Munkey01 in forum C++ Programming
    Replies: 4
    Last Post: 02-08-2003, 08:35 AM

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