Search code examples
clarge-file-support

C - creating new file with custom filename


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?


Solution

  • 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 .