9
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;