Hey guys i need to check that exatcly 4 arguments are entered via the command line but i if i pass more or only 3 command line arguments it runs when i want it to sto executing.
And would the if statements == NULL be a good way of checking whether the files exist and if not the case exit the program?
Could someone double check if iv done these correctly?
Code:
int commandLineArguments(int argc, char **argv)
{
/* makes a local copy of argc and **argv */
FILE *customer, *stock, *logfile;
/* declares a pointer to each file*/
/* test if 4 arguments exist including the exucutable*/
if(argc !=4)
{
printf("please enter 3 cmd arguments!\n");
return 1;
/* stop pocessing*/
}
/* attempt to open and read arg 1*/
customer = fopen(argv[1], "r");
/* check if custome.csv exists*/
if(customer ==NULL)
{
printf("cannot open %s for reading\n", argv[1]);
return 1;
/* can't go on*/
}
/* try to open stock.csv and read it*/
stock = fopen(argv[2], "r");
/* if stock.csv does not exist error*/
if(stock == NULL)
{
printf("cannot open %s for reading\n",argv[2]);
return 1;
/* stop processing*/
}
/* open logfile for reading */
logfile = fopen(argv[3], "r");
/* check if logfile exists*/
if(logfile == NULL)
{
printf("cannot open %s for reading\n", argv[3]);
return 1;
/* stop processing*/
}
/* close all files */
/* checking for closing file errors */
if(fclose(customer)!=0 || fclose(stock) !=0 || fclose(logfile) !=0)
{
fprintf(stderr, "Error in closing files\n");
}
return 0;
}