First time using command line args in C, and can't get it to work. Here's what I got:
this should get the one and only command line argument, which is a filename, then it calls openFile with that. If there is no command line arguments, it gets it from within the program.
Code:
if(argc == 2){
if(openFile(argv[1]) == 1){
countWords();
showResults();
closeFile();
}
else{
printf("Does not exist\n");
return EXIT_FAILURE;
}
}
else{
getFileName();
if(openFile(fileName) == 1){
countWords();
showResults();
closeFile();
}
else{
printf("Does not exist\n");
return EXIT_FAILURE;
}
}
and here is my open file
Code:
int openFile(char* filename){
/*Open the file*/
if((inFile = fopen(fileName, "r")) == NULL){
return 0;
}
return 1;
}
The code for getting the filename within the program works fine, it just tells me the file does not exist whenever it gets run with the command line arg. Any suggestions?