Search code examples
cfilefile-ioio

Is there a way to pass a string variable to remove() in c


Okay, so this is the function I'm using remove():

int fmove(const char * filepth, const char * destpth)
{
    FILE * fp;
    fp = fopen(filepth, "r+");

    FILE * fpdest;
    fpdest = fopen(destpth, "w");

    if ((fp != NULL) && (fpdest != NULL))
    {
        char fpdata[999];
        fgets(fpdata, 999, fp);
        fputs(fpdata, fpdest);

        remove(filepth);
    } else
    {
        prtmessage("ERROR", "Cannot move file!");
        return 1;
    }

    return 0;
}

but the remove() returns -1. I know that arrays return their pointers (am I telling it right?), but is there a way to pass the string in the filepath to remove()?


Solution

  • From the C Standard

    If the file is open, the behavior of the remove function is implementation-defined.

    You need to close the file before calling remove().