user2517789
user2517789

Reputation: 1

How do I get the command line to read file paths and variables in C

I'm trying to make a program run from the command line in C and it is going to require typing in a file directory and inputing variables. I'm trying to use the stricmp function to read the code typed into the command line but I'm not sure how to get it to read the file (I read that for a file path you're supposed to put "r") and perform the calculations. I was thinking scanf maybe and also I need to be able to input variables. If anyone could look at my code and give me a couple pointers I would much appreciate it. Thanks!

int main(int argc, char *argv[])
{
    int i = 0;

    for(i < argc; i++;)

    {

    if(0 == stricmp("Interrogator MLV_PLV_179 r %s %s", argv[i]))
    {
        InterrogatorProtocol1553Flag = TRUE;
        // Set DMV-179
        Interrogator_OFP_Select = INT_OFP_DMV_179;
        // Set MLV/PLV  (Dim S-Record File)
        DirectFlashProgFlag = FALSE;
        // Set Fill Type to 1
        Interrogator_PIR_OFP_FOP = 3;
        Interrogator_PIR_ProgramID = (Interrogator_PIR_T_I     << 11) |
                                             (Interrogator_PIR_OFP_FOP <<  5) |
                                              Interrogator_PIR_RT_Address;
    }

    else if(0 == stricmp("Interrogator DirectFlash r r %s %s", argv[i]))
    {
        InterrogatorProtocol1553Flag = TRUE; 
        Interrogator_OFP_Select = INT_OFP_DMV_179;
        DirectFlashProgFlag = TRUE;
        CreateInterrogatorFlashImage;
    }

    else if(0 == stricmp("Interrogator MLV_PLV_183 r %s %s", argv[i]))
    {
        InterrogatorProtocol1553Flag = TRUE; 
        // Set DMV-183
        Interrogator_OFP_Select = INT_OFP_DMV_183; 
        // Set MLV/PLV  (Dim S-Record File)
        DirectFlashProgFlag = FALSE;
        // Set Fill Type to 6
        Interrogator_PIR_OFP_FOP = 6;
        Interrogator_PIR_ProgramID = (Interrogator_PIR_T_I     << 11) |
                                             (Interrogator_PIR_OFP_FOP <<  5) |
                                              Interrogator_PIR_RT_Address;
    }

    else if(0 == stricmp("Interrogator SRecord r r %s %s", argv[i]))
    {
        InterrogatorProtocol1553Flag = TRUE; 
        Interrogator_OFP_Select = INT_OFP_DMV_183;
        DirectFlashProgFlag = TRUE;
        CreateIntermediateSRecordFile;
    }

    else
    {
        fprintf(stderr, "Unknown parameter: %s", argv[i]);
    }

    }

    return 0;
}

Upvotes: 0

Views: 2679

Answers (2)

James Holderness
James Holderness

Reputation: 23001

Each parameter on the command line is passed in as a separate argv entry. Assuming I've understood what your parameters are supposed to mean, I would expect your code to look something more like this:

if(0 == stricmp("Interrogator", argv[i]))
    InterrogatorProtocol1553Flag = TRUE;
else if(0 == stricmp("MLV_PLV_179", argv[i]))
    Interrogator_OFP_Select = INT_OFP_DMV_179;
else if(0 == stricmp("MLV_PLV_183", argv[i]))
    Interrogator_OFP_Select = INT_OFP_DMV_183; 
else if(0 == stricmp("DirectFlash", argv[i]))
    DirectFlashProgFlag = TRUE;
...

And before you start the loop you would initialise those variables with default values that you expect to be used when the various parameters haven't been set.

If one of your parameters is a filename, then you need some way to indicate which one it is. One easy option would be to make it the last value on the command line. So you would get the filename like this:

filename = argv[argc-1];

In that case your loop that is reading all the other parameters should only go up to argc-1 otherwise the filename will be interpreted as an unrecognised parameter and you would get an error.

Upvotes: 1

max
max

Reputation: 4348

Your question is not clear.

If you need to input from the standard input (keyboard) use scanf. If you need to input from a file, use fscanf. There is only one difference between them: fscanf has an extra first FILE* argument which is the pointer to a structure which represents a file. You can get it for your file like this:

FILE* f = fopen("C:/file.txt", "r"); // 'r' means the file is used for reading

Upvotes: 0

Related Questions