Command Line Arguments

This is a discussion on Command Line Arguments within the C Programming forums, part of the General Programming Boards category; Getting segmentation fault errors and I can't figure out why. All I'm currently doing is asking for a command line ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    11

    Command Line Arguments

    Getting segmentation fault errors and I can't figure out why.

    All I'm currently doing is asking for a command line argument and trying to print the data.

    Code:
    int main(char *fileName){
    
    	printf("Welcome\n\n");
    
    	
    
    	//if (strlen(fileName) > 0){
    
    	//	file = fopen(fileName, "r");
    
    	//}
    
    	printf("%s", fileName);
    }
    Thanks in advance,
    Lang

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The first parm is the count of args (an int) and the second is a pointer to an array of strings.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    from FAQ
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    11
    Thanks for the help you two! I appreciate it.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    and dont forget to close the file =)

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Heres how i process my command line arguments:

    Code:
    int main(int argc, char* argv[])
    {
        char fName[1024] = "";
        int width = 0;
        int height = 0;
        for(int i = 0; i < argc; ++i)
        {
            if(argv[i][0] == '-')
            {
                switch(argv[i][1])
                {
                    case 'w':
                    case 'W':
                        width = atoi(argv[++i]);
                    break;
    
                    case 'h':
                    case 'H':
                        height = atoi(argv[++i]);
                    break;
                }
            //If it is not a command option (an option starting with '-'),
            //its probably the name of the file that was dragged onto the exe
            } else if(i == 1)
                strncpy(fName, argv[i], sizeof(fName));
        }
        printf("File: &#37;s\n", fName);
        printf("Width: %i\n", width);
        printf("Height: %i\n", height);
    
        if(strlen(fName))
        {
            //Do something
        }
        return 1;
    }
    This lets you process command lines such as:

    c:\myApp.exe test.txt -w 1024 -h 768
    c:\myApp -w 1024 -h 768
    c:\myApp test.txt
    Last edited by 39ster; 03-29-2008 at 02:18 AM.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    if user by mistake does not put anything after -h or -w - you will get out of bounds access to the argv array

    when you increase you i var - check that it is still less than argc

    ps. Note also that strncpy
    If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string.
    so you better do it yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Does char fName[1024] = ""; set all the elements to 0?

    EDIT: I just tried
    Code:
        for(unsigned int i = 0; i < sizeof(fName); ++i)
            printf("&#37;i\n", fName[i]);
    And it does.

    Ok i know use this:

    Code:
    int main(int argc, char* argv[])
    {
        char fName[1024] = "";
        int width = 0;
        int height = 0;
        for(int i = 0; i < argc; ++i)
        {
            if(argv[i][0] == '-')
            {
                switch(argv[i][1])
                {
                    case 'w':
                    case 'W':
                        if(++i < argc)
                          width = atoi(argv[i]);
                    break;
    
                    case 'h':
                    case 'H':
                        if(++i < argc)
                          height = atoi(argv[i]);
                    break;
                }
            //If it is not a command option (an option starting with '-'),
            //its probably the name of the file that was dragged onto the exe
            } else if(i == 1)
                strncpy(fName, argv[i], sizeof(fName));
        }
        printf("File: %s\n", fName);
        printf("Width: %i\n", width);
        printf("Height: %i\n", height);
    
        if(strlen(fName))
        {
            //Do something
        }
        return 1;
    }
    Last edited by 39ster; 03-29-2008 at 02:34 AM.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    And it does.

    Ok i know use this:
    still - when user enters string more than 1024 char - your strncpy will overwrite the whole array leaving it not nul-terminated
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 01:11 PM

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