I'm having always the error "Segmentation fault (core dumped)"" every time I try this a function:
int CheckFile(char * filename){
FILE * bd = fopen(filename, "r");
if(bd == NULL){
fclose(bd);
return -1;
}else{
fclose(bd);
return 0;
}
}
function calls:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_FILE_NAME "file.txt"
int CheckFile(char * filename);
int main(int argc, char ** argv[]){
char * name_of_file == NULL;
if(argc > 1){
printf("argc > 1\n");
for(i=0;argc>i;i++){
if(strcmp(argv[i],"-f")==0){
name_of_file = argv[i+1];
if(CheckFile(name_of_file) != 0)
printf("Can't find the file "%s".", name_of_file);
}
if(name_of_file == NULL){
if(CheckFile(DEFAULT_FILE_NAME) != 0);
printf("Can't find the default file \""DEFAULT_FILE_NAME"\".");
}
}
By my troubleshoots I would say the problem is on "char * filename", but can't find a way out of this. Can someone give me a hand? I would be thankful.