Command line argc and argv help.

This is a discussion on Command line argc and argv help. within the C Programming forums, part of the General Programming Boards category; I'm building a command line interface for a program and the program doesn't seem to be reading the functions inside ...

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    9

    Command line argc and argv help.

    I'm building a command line interface for a program and the program doesn't seem to be reading the functions inside the "else if" statements. The program compiles and everything but nothing happens when I type the arguments into the command line. I think there might be something wrong with my argc and argv code but I'm not sure. Any help would be much appreciated. Thanks guys!

    Code:
    int main(int argc, char *argv[])
    {
        FILE   *ISP_file;
        FILE   *SP_file;
        //FILE   *TempFile;
     
        int i = 0;
    
        for(i = 0;i < argc; i++)
      
        {
     
            if(0 == stricmp("Interrogator", argv[i]))
            {
                InterrogatorProtocol1553Flag = TRUE;
            }
      
           else if(0 == stricmp("MLV_PLV_179", argv[i]))
            {
               Interrogator_OFP_Select = INT_OFP_DMV_179;
               DirectFlashProgFlag = FALSE;
               //ISP_file = fopen(FopISP_DataFile, "rb");
               SP_file  = fopen(FopSP_DataFile, "rb");
               //TempFile = fopen(TempFilePath, "wb");
               Execute1553();
             }
      
           else if(0 == stricmp("MLV_PLV_183", argv[i]))
            {
                Interrogator_OFP_Select = INT_OFP_DMV_183;
                DirectFlashProgFlag = FALSE;
                //ISP_file = fopen(FopISP_DataFile, "rb");
                SP_file  = fopen(FopSP_DataFile, "rb");
                //TempFile = fopen(TempFilePath, "wb");
                Execute1553();
            }
      
           else if(0 == stricmp("DirectFlash", argv[i]))
            {
                 Interrogator_OFP_Select = INT_OFP_DMV_179;
                 DirectFlashProgFlag = TRUE;
                 ISP_file = fopen(FopISP_DataFile, "rb");
                 SP_file  = fopen(FopSP_DataFile, "rb");
                //TempFile = fopen(TempFilePath, "wb");
                 Execute1553();
            }
      
            else if(0 == stricmp("SRecord", argv[i]))
            {
                 Interrogator_OFP_Select = INT_OFP_DMV_183;
                 DirectFlashProgFlag = TRUE;
                 ISP_file = fopen(FopISP_DataFile, "rb");
                 SP_file  = fopen(FopSP_DataFile, "rb");
                 //TempFile = fopen(TempFilePath, "wb");
                 Execute1553();
            }
      
            else
            {
                fprintf(stderr, "Unknown parameter: %s", argv[i]);
            }
        }
    Last edited by jwernig; 07-15-2013 at 06:39 AM.

  2. #2
    Registered User HelpfulPerson's Avatar
    Join Date
    Jun 2013
    Location
    Over the rainbow
    Posts
    288
    Why do you do 0 == value. Usually people enter value == 0. That shouldn't matter. I can't really help you because I don't know what stricmp is, it's definitely not a standard function, and I don't know how it handles strings. Just use an actual standard function like this

    Code:
    elseif(!strcmp("SRecord", argv[i]))
    That should work.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by HelpfulPerson View Post
    Why do you do 0 == value.
    It is done so the erroneous construction
    Code:
    0 = value
    will cause the compilation error on any compiler, not a warning on smart one.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The only mistake I see in your program is that you start your for loop at 0 whereas it should start at 1.
    argv[0] contains the program name (or possibly an empty string).

    EDIT:
    When you way that nothing happens, what do you expect to happen?
    It's hard to see how the files will be read from since the FILE*'s are local to main.
    (Could they be shadowing global FILE*'s?)
    Last edited by oogabooga; 07-15-2013 at 09:36 AM.

  5. #5
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    Quote Originally Posted by oogabooga View Post
    The only mistake I see in your program is that you start your for loop at 0 whereas it should start at 1.
    argv[0] contains the program name (or possibly an empty string).

    EDIT:
    When you way that nothing happens, what do you expect to happen?
    It's hard to see how the files will be read from since the FILE*'s are local to main.
    (Could they be shadowing global FILE*'s?)
    Basically I want to be able to enter one or two filepaths (depending on the parameter) into the command line after the parameter is entered and then have those files run through the Execute 1553 function if that makes sense.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you should first determine - which part of your code is at fault

    So start by
    Code:
    int main(int argc, char *argv[])
    {
        FILE   *ISP_file;
        FILE   *SP_file;
        //FILE   *TempFile;
    
        int i = 0;
    
        for(i = 0;i < argc; i++)
    
        {
    
            if(0 == stricmp("Interrogator", argv[i]))
            {
                printf ("Parameter %d read - Interrogator", i);
            }
    
            else if(0 == stricmp("MLV_PLV_179", argv[i]))
            {
                printf ("Parameter %d read - MLV_PLV_179", i);
                SP_file  = fopen(FopSP_DataFile, "rb");
                if(!SP_file)
                {
                    printf ("Could not open file %s", FopSP_DataFile);
                }
            }
            etc
    And run such program with different combination of parameters

    Do you close your files somewhere?

    Why don't you pass the handles to the open files to the function which should process them?

    When you check that your parameter parsing code works as desired - add calls to your processing function - making just debugging prints inside it making sure that all passed parameters are correct.

    And only then add the code which actually does the work.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    Quote Originally Posted by vart View Post
    you should first determine - which part of your code is at fault

    So start by
    Code:
    int main(int argc, char *argv[])
    {
        FILE   *ISP_file;
        FILE   *SP_file;
        //FILE   *TempFile;
    
        int i = 0;
    
        for(i = 0;i < argc; i++)
    
        {
    
            if(0 == stricmp("Interrogator", argv[i]))
            {
                printf ("Parameter %d read - Interrogator", i);
            }
    
            else if(0 == stricmp("MLV_PLV_179", argv[i]))
            {
                printf ("Parameter %d read - MLV_PLV_179", i);
                SP_file  = fopen(FopSP_DataFile, "rb");
                if(!SP_file)
                {
                    printf ("Could not open file %s", FopSP_DataFile);
                }
            }
            etc
    And run such program with different combination of parameters

    Do you close your files somewhere?

    Why don't you pass the handles to the open files to the function which should process them?

    When you check that your parameter parsing code works as desired - add calls to your processing function - making just debugging prints inside it making sure that all passed parameters are correct.

    And only then add the code which actually does the work.
    How do I pass the handles of the files to the Execute1553 function?

  8. #8
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    How do I pass the handles of the files to the Execute1553 function?

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    you declare function like this

    Code:
    int Execute1553(FILE   *ISP_f, FILE   *SP_f);
    and call it like this
    Code:
    if(Execute1553(ISP_file,SP_file) != 0)
       printf("Calculation failed\n");
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    Can I do that inside the strcmp parameter brackets?

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by jwernig View Post
    Can I do that inside the strcmp parameter brackets?
    Calling? yes
    Declaring - better do it in h file of before main in the c-file (depends where the function body is located)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    125
    You should start looping at i = 1 because the first arguments argv[0] is the program name as you write at the terminal
    Code:
    int  i = 1;
    for ( i = 1 ; i < argc ; i++ ){
    //code
    }
    Last edited by Aslaville; 07-15-2013 at 02:19 PM.
    In C++14 you just write "auto auto(auto auto) { auto; }".
    The compiler infers the rest from context.

  13. #13
    Registered User
    Join Date
    Jul 2013
    Posts
    9
    How does this look?

    Code:
    else if(!strcmp("SRecord", argv[i])) 
        {
            Interrogator_OFP_Select = INT_OFP_DMV_183;
            DirectFlashProgFlag = TRUE;
            if(Execute1553(ISP_file, SP_file) != 0)    
            printf("Calculation failed\n"); 
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting command line char *argv[] to int
    By Winslow in forum C Programming
    Replies: 2
    Last Post: 01-30-2011, 01:41 PM
  2. command line arguments using argc and argv.
    By Cudarich in forum C Programming
    Replies: 2
    Last Post: 12-05-2004, 06:32 AM
  3. argv and argc command line params
    By iain in forum C Programming
    Replies: 4
    Last Post: 10-09-2002, 04:15 PM
  4. command line argv argc help?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-08-2002, 06:07 AM
  5. command line program, something wrong in the argv
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-26-2001, 09:36 AM

Tags for this Thread


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