0

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.

  • try `const char * filename` – Jean-François Fabre Oct 26 '18 at 12:13
  • also don't `fclose(bd);` if bd is null. – Jean-François Fabre Oct 26 '18 at 12:14
  • 4
    Does your compiler really compile this? `int main(int argc, char ** argv[])` There should be some warning about wrong level of indirection for `argv[i]` – Gerhardh Oct 26 '18 at 12:14
  • 1
    and don't do `name_of_file = argv[i+1];` if `i==argc-1` :) – Jean-François Fabre Oct 26 '18 at 12:14
  • It's time to learn some [basic debugging](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Run your program in a debugger and it should at least tell you *where* the SEGFAULT happens. Then you can find out *WHY* – Gerhardh Oct 26 '18 at 12:16
  • Please also provide all the important information: What input do you provide? How do you call your program? – Gerhardh Oct 26 '18 at 12:18
  • This can't be your real code. It can't compile... – Support Ukraine Oct 26 '18 at 12:20
  • `char * name_of_file == NULL;` ? Sure about the `==` – Support Ukraine Oct 26 '18 at 12:22
  • Writing `for(i=0;argc>i;i++){` is a cackhanded, unorthodox way of writing `for (i = 0; i < argc; i++) {`. This is the conventional notation for a `for` loop counting from `0` up to a limit (`argc` in this case). Other problems have been pointed out to you in comments and answers (including that you probably need `for (int i = 0; i < argc; i++) {` in the context of your program, which does not visibly define/declare `i`). – Jonathan Leffler Oct 26 '18 at 12:42

2 Answers2

1

The code doesn't compile properly. The definition of main() is wrong:

int main(int argc, char ** argv[]) {

It needs to be

int main(int argc, char **argv) {

Also this

char * name_of_file == NULL;

Needs to be

char * name_of_file = NULL;

Because we're doing initialization here, not comparison.

Also, that print doesn't compile for me either:

printf("Can't find the file "%s".", name_of_file);

It seems like your escape sequences/format specifiers got mangled somehow. Try

printf("Can't find the file %s.", name_of_file);

Instead.

Next, i isn't defined. Do you have it as a global variable that you haven't shown in your snippet? Either way you can just put it here:

for (int i = 0; argc > i; i++) {

instead of

for(i=0;argc>i;i++){

Also, this if here does nothing:

if(CheckFile(DEFAULT_FILE_NAME) != 0);

Because of that semicolon at the end, you probably want to remove that.

Last but not least, there are two } missing at the end of your main function.

It seems like you have problems with debugging your code. Consider using an IDE that will show you compilation errors. Also, you should not try to fclose the file if it didn't open (fopen returning NULL).

Blaze
  • 16,736
  • 2
  • 25
  • 44
1

This has undefined behavior and should be expected to crash or worse:

if(bd == NULL){
    fclose(bd);

A null pointer is not a valid argument to fclose. Just remove the call to fclose there.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711