1

I'm writing a function/method in C that is supposed to do the following:

/** * Recieves the list of arguments and copies to conffile the name of * the configuration file. * Returns 1 if the default file name was used or 2 if * the parameter -f existed and therefor the specified name was used. Returns 0 if there is a -f * parameter but is invalid. (last argument)
*/

Basically one of these two option will be "asked" by the program:

1- controller -f FCFS|LOOK

2- controller FCFS|LOOK

If the second is asked, we enter the case of using the default name.

 int get_conf_name(char* argv[], char* conffile) { 

// Searches for -f 

 s = strstr(*argv, "-f");


if(s != NULL){

      if(//its valid){
 strcpy(conffile, //the name of the file which comes after -f

 return 2

 }

      else return 0

 }

 else{

 strcpy(confile, "config.vss")

 return 1

 }

 }

The problem here is how do I get the word after -f and copy it to confile? And, can I access argv the same way I access conffile, since one of them is an array? I thought of using a for loop and a strlen, but that would be a lot of unecessary work for the computer wouldn't it?

3
  • That's not how strstr works. strstr looks for a substring (needle) in another string (haystack). What you want instead is looking for a whole string in an array of strings. You should use a loop. Commented Oct 27, 2013 at 16:48
  • btw: options are "passed to" (not "asked by") the program. Unless you're referring to the caller program. Commented Oct 27, 2013 at 16:51
  • @Giulio Franco Oh thanks :D By asked I meant there's two possible options for the user to ask the program Commented Oct 27, 2013 at 17:06

1 Answer 1

3

I think the assignment expects you to go through the individual arguments one by one, comparing them to "-f".

  • If you see no -f, you know that the default file needs to be used
  • If you see the flag in the final position, you know that the -f is invalid
  • If you see the flag in the position k, then the file name will be in argv[k+1]

The skeleton of your program should look like this:

bool foundFlag = false;
for (int i = 1 ; i < argc ; i++) {
    if (strcmp(argv[i], "-f") == 0) {
        if (i == argc-1) {
            // Error
        } else {
            // argv[i+1] is the file name;
        }
        foundFlag = true;
        break;
    }
}
if (!foundFlag) {
    // Default name is used
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, the skeleton is very clean and understandable :D So now, I can use argv[i+1] directly on strcpy right? But won't that [i+1] point to the space instead of the first char of the name?
@ipg24 That's the thing - spaces and tabs are taken out of the original command string when it is tokenized. Only non-whitespace characters are kept.
Understood :) Thanks a lot, now I know how to structure and write the whole thing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.