Right, I’ve got this code:
if(argc>1){
FILE * pFile = fopen(argv[1],"rb");
perror("");
}else{
FILE * pFile = fopen("hardcoded","rb");
}
if(pFile==NULL){
puts("Unable to open source file");
return -1;
}
However, I get this weird output:
Success
Unable to open source file
Weirdlier, if I do this:
if(argc>1){
FILE * pFile = fopen(argv[1],"rb");
perror("");
}else{
FILE * pFile = fopen("hardcoded","rb");
}
FILE * pFile = fopen("hardcoded","rb");
if(pFile==NULL){
puts("Unable to open source file");
return -1;
}
Where hardcoded exists, it all works fine!
What the blazes does that mean?
Compiling with GCC4 on Ubuntu
I’m surprised your code compiles, since the you are declaring
FILE *pFilescoped to the if and the else blocks. If you’ve declare it prior to that, then remove theFILE*text in front of the assignments in the if/else blocks.