Home > Software engineering >  Taking only one command line argument in putty using c
Taking only one command line argument in putty using c

Time:11-10

Hi i want to make code that accepts only one input file by c. For example i want input file's name that is shortinput.txt ! so I will take one command line argument at putty and I also want to make the code get only one argument. If the number of command-line arguments is not 1 (zero or more than 1), then should print a usage string on the display and exit the program. The usage string looks like this: usage: ./hw2 input_filename

The problem is here. In the code below I want to print error not only the argument is empty but more than one!

int main(int argc, char* argv[]){
 if(argv[1]==NULL||argv[2]||argv[3])//where i want to fix  
 {
     printf("usage: ./hw2 input_filename"); 
 }  
 FILE *fp =fopen(argv[1],"r"); 
 if(fp==NULL){
     printf("The input file does not exist.\n"); 
    } 
 }

CodePudding user response:

You want to check how many arguments were passed. You have argc parameter for that.

if(argc != 2)
{
    printf("usage: ./hw2 input_filename");
    /* do more (exit etc) */
}
  • Related