I have been trying to do error checking with my code to make sure that the argument given is a valid file. The following code:
int main(int argc, char *argv[]){
for(int i = 1, i <=argc, i++){
FILE *fileIn = fopen(argv[i],"r");
if(fileIn == NULL){
fprintf(stderr,"The file %s doesn't exist.",fileIn);
}
else if (fileIn != NULL){
do a bunch of stuff, including printing out values from a struct
}
The problem I am having is when I run my program I get this as an output:
The (null) file doesn't exist
Output from else if loop
So basically, it is saying that fileIn is null and is not null at the same time. It is reading the file normally and doing it as it should, but it runs through the error each time. Is there any way to assign a pointer to argv[i or something? How is this happening?
The code is looping one too many times. The
<= argcis evaluating one more argument than actually exists. It should be:Also, the printf for the non-existent file should be as follows (it needs the name):