C - creating new file with custom filename

185 views Asked by At

I want to create new file with custom filename. I made this function to do this task:

int addfile(char* name)//return 0 if cannot create file
{
    FILE* fn = fopen(name, "r");
    if(fn == NULL)
    {
        return 0;
    }
    fclose(fn);
    addtotab(name, 0);
    return 1;
}

Unfortunately fn variable is always NULLptr so its early return 0. Is any better idea to do this than with fopen?

1

There are 1 answers

0
Antonio Petricca On BEST ANSWER

You should change

FILE* fn = fopen(name, "r");

in

FILE* fn = fopen(name, "w");

accordingly to the documentation at https://man7.org/linux/man-pages/man3/fopen.3.html .