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?
strstrworks.strstrlooks 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.