Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a file in C

Tags:

c

I have written a program that manipulates files and I sometimes to delete a file. So I searched and found the remove() function in the stdio header file in C.

The problem is that sometimes works and sometimes not. Sometimes the file is deleted, other perror() shows a message that says permission denied but I have not specified any special permissions on the file. As a matter of fact the file has been created a little before by another function. Any special conditions that I have to consider?

Here is the load function that creates the file:

...
int loadF ( const char *filename, plist_t pl, int size, int createFlag ) {
    FILE *pFile = NULL;
    pnode_t pn = NULL;
    int fsize;
    int total;
    int i;

    // Get file stream.
    pFile = fopen ( filename, "rb" );
    if ( !pFile ) { // File does not exist.
        if ( createFlag ) {
            if ( !createF ( filename ) ) {
                return 0;   // fail
            }
        } else {    // abort
            perror ( "loadF:fopen" );
            return 0;
        }
    }

    // Confirm that we have opened the file stream.
    if ( !pFile ) {
        pFile = fopen ( filename, "rb" );
        if ( !pFile ) {
            perror ( "loadF:fopen:" );
            return 0;
        }
    }

    // Check if list has not been initialized.
    if ( pl == NULL ) {
        fclose ( pFile );
        pFile = NULL;
        return 0;   // abort
    }

    // Get the size of the file.
    fseek ( pFile, 0, SEEK_END );
    fsize = ftell ( pFile );
    rewind ( pFile );

    // Check if the file is empty.
    if ( !fsize ) {
        fclose ( pFile );
        pFile = NULL;
        return 1;   // No data to load, continue.
    }

    // Get the total number of structures in the file.
    total = fsize / size;

    // Allocate memory for a node to transfer data.
    pn = (pnode_t) malloc ( sizeof (node_t) * sizeof (char) );
    if ( !pn ) {
        fclose ( pFile );
        pFile = NULL;
        perror ( "loadF:malloc" );
        return 0;
    }

    // Copy from file to list every structure.
    for ( i = 1; i <= total; i++ ) {
        if ( feof ( pFile ) ) {
            printf ( "OUT!" );
            break;
        }
        printf ( "g" );
        fread ( pn->key, size, 1, pFile );
        printf ( "f\n" );
        if ( ferror ( pFile ) ) {
            fclose ( pFile );
            pFile = NULL;
            perror ( "loadF:fread" );
            return 0;
        }
        addfirst ( pl, pn->key ); // Maybe we have to allocate memory with malloc every time?
        // Debug with a for loop in the nodes of the list to see if data are OK.
        printf ( "cid = %d\n", pl->head->key->card.cid );
        printf ( "limit = %5.2f\n", pl->head->key->card.limit );
        printf ( "balance = %5.2f\n", pl->head->key->card.balance );
    }

    // Close the stream.
    if ( pFile ) {
        fclose ( pFile );
        pFile = NULL;
    }

    // Deallocate transfer memory.
    if ( pn ) {
        free ( pn );
    }

    // Exit
    return 1;
}

And here is the function that uses remove:

int saveF ( const char *filename, plist_t pl, int size ) {
    FILE *pFile = NULL;     // Pointer to the file structure.
    pnode_t pn = NULL;      // Pointer to a node of a list.


    // Delete the specified file - on success it returns 0.
    if ( remove (filename) == -1 ) {
        perror ( "saveF:remove" );
        return 0;
    }

    // Re-create the file (but now is empty).
    if ( !createF (filename) ) {
        return 0;
    }

    // Get the file stream.
    pFile = fopen ( filename, "ab" );
    if ( !pFile ) {
        perror ( "saveF:fopen" );
        return 0;
    }

    // Check if list is not empty.
    if ( isEmpty ( pl ) ) {
        fclose ( pFile );
        pFile = NULL;
        return 0;           // Abort
    }

    // Traverse the list nodes and save the entity that the key points to.
    for ( pn = pl->head; pn != NULL; pn = pn->next ) {
        fwrite ( (pccms_t)(pn->key), size, 1, pFile );
        if ( ferror ( pFile ) ) {
            fclose ( pFile );
            pFile = NULL;
            perror ( "saveF:fwrite" );
            return 0;
        }
    }

    // Close the stream.
    if ( pFile ) {
        fclose ( pFile );
        pFile = NULL;
    }

    // Exit
    return 1;
}

I use Windows XP.

like image 374
Ponty Avatar asked Dec 20 '09 09:12

Ponty


People also ask

How can we delete a file in C?

Delete File in C Programming To delete a file using C language, use remove() function of stdio. h. remove() function takes file name (or path if not located in the same location) as argument and deletes the file. remove() returns 0 if the file is deleted successfully, else it returns a non-zero value.

What is remove () in C?

The remove function in C/C++ can be used to delete a file. The function returns 0 if files is deleted successfully, other returns a non-zero value. #include<stdio.h> int main() {

How do I delete files from command line?

To delete a file, use the following command: del "<filename>" . For example, to delete Test file. txt , just run del "Test File.


3 Answers

You haven't specified the platform you are using, but on Windows, the file shouldn't be open when you try to delete it. (Things work a bit differently on Unix-style systems and a delete is almost always possible even if the file is open.)

like image 67
Greg Hewgill Avatar answered Oct 05 '22 18:10

Greg Hewgill


Case that you can't remove file can also depend on the file being locked by another process. In your situation it could be your function creating files. It also depends on the platform you use. If it's Windows, try to use Unlocker and than retry to delete a file.

like image 36
andrzejkaszkowiak Avatar answered Oct 05 '22 19:10

andrzejkaszkowiak


There are 3 reasons in my opinion:

  1. You don't have permission to delete the file.
  2. The file is in use by another function.
  3. You have to have writing persmissions in the directory that this file is located.
like image 34
Alex Ntousias Avatar answered Oct 05 '22 20:10

Alex Ntousias