remove() [ to delete a file ] fails

S

sam_cit

Hi Everyone,

I'm using remove() function to delete a file, and i observed the
following behavior,

Concerned file : sample.txt
Operation : i open the file in read mode and don't close the file.
remove() returns -1 and the file is not deleted.

The above operation is successful when i close the file just before
the delete. Does having a file handle to a file impact remove() ? Is
this documented somewhere?

Code sample :

#include <stdio.h>

int main()
{
FILE *p;
p = fopen("c:\\sam.txt","r");
if(p == NULL)
{
printf("file open failed...\n");
}
else
{
printf("file open success...\n");
}
//printf("close result is %d\n",fclose(p));
printf("result of deleteion is : %d\n",remove("c:\\sam.txt"));
}
 
T

T.M. Sommers

Hi Everyone,

I'm using remove() function to delete a file, and i observed the
following behavior,

Concerned file : sample.txt
Operation : i open the file in read mode and don't close the file.
remove() returns -1 and the file is not deleted.

The above operation is successful when i close the file just before
the delete. Does having a file handle to a file impact remove() ? Is
this documented somewhere?

Sect. 7.19.4.1 of the standard says, "If the file is open, the
behavior of the remove function is implementation-defined."
 
B

Bill Pursell

Hi Everyone,

I'm using remove() function to delete a file, and i observed the
following behavior,

Concerned file : sample.txt
Operation : i open the file in read mode and don't close the file.
remove() returns -1 and the file is not deleted.

The above operation is successful when i close the file just before
the delete. Does having a file handle to a file impact remove() ? Is
this documented somewhere?

Code sample :

#include <stdio.h>

int main()
{
FILE *p;
p = fopen("c:\\sam.txt","r");
if(p == NULL)
{
printf("file open failed...\n");
}
else
{
printf("file open success...\n");
}
//printf("close result is %d\n",fclose(p));
printf("result of deleteion is : %d\n",remove("c:\\sam.txt"));
}

The error your seeing may be a result of you attempting
to remove a file that doesn't exist. What you're trying to do
works for me:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
FILE *p;
char *filename;

filename = (argc > 1) ? argv[1] : "foo";

p = fopen(filename, "r");
if(p == NULL)
perror(filename);
else
printf("opened %s\n", filename);

if (argc > 2)
printf("close result is %d\n", fclose(p));

printf("result of remove() is : %d\n", remove(filename));

return EXIT_SUCCESS;;
}
$ gcc -Wall -pedantic a.c
$ touch foo
$ ./a.out
opened foo
result of remove() is : 0
$ touch foo
$ ./a.out foo no close
opened foo
close result is 0
result of remove() is : 0
 
S

sam_cit

The error your seeing may be a result of you attempting
to remove a file that doesn't exist. What you're trying to do
works for me:

In my case, i made sure the file does exists, and if i close the
file, the remove() works fine.
But i used Microsoft VC++ 6.0 and i see that you are on unix. Problably
its implementation dependent as pointed out...

Any other comments???
 
R

Richard Heathfield

(e-mail address removed) said:
In my case, i made sure the file does exists, and if i close the
file, the remove() works fine.
But i used Microsoft VC++ 6.0 and i see that you are on unix. Problably
its implementation dependent as pointed out...

There's no "probably" about it. If the Standard says it's
implementation-defined, then it's implementation-defined.

See 4.9.4.1 of ANSI C or 7.19.4.1 of ISO/IEC 9899:1999.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Bill said:
Hi Everyone,

I'm using remove() function to delete a file, and i observed the
following behavior,

Concerned file : sample.txt
Operation : i open the file in read mode and don't close the file.
remove() returns -1 and the file is not deleted.

The above operation is successful when i close the file just before
the delete. Does having a file handle to a file impact remove() ? Is
this documented somewhere?

Code sample :

#include <stdio.h>

int main()
{
FILE *p;
p = fopen("c:\\sam.txt","r");
if(p == NULL)
{
printf("file open failed...\n");
}
else
{
printf("file open success...\n");
}
//printf("close result is %d\n",fclose(p));
printf("result of deleteion is : %d\n",remove("c:\\sam.txt"));
}

The error your seeing may be a result of you attempting
to remove a file that doesn't exist. What you're trying to do
works for me:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
FILE *p;
char *filename;

filename = (argc > 1) ? argv[1] : "foo";

p = fopen(filename, "r");
if(p == NULL)
perror(filename);
else
printf("opened %s\n", filename);

if (argc > 2)
printf("close result is %d\n", fclose(p));

printf("result of remove() is : %d\n", remove(filename));

return EXIT_SUCCESS;;
}
$ gcc -Wall -pedantic a.c
$ touch foo
$ ./a.out
opened foo
result of remove() is : 0
$ touch foo
$ ./a.out foo no close
opened foo
close result is 0
result of remove() is : 0
I take it you're running on linux or another *nix. On those, you
can remove files that are referenced.
On other systems, such as Windows, you can't in many circumstances.

As far as C is concerned, the behavior when you want to remove a file
you have open is implementation defined.
 
K

Kenneth Brody

Hi Everyone,

I'm using remove() function to delete a file, and i observed the
following behavior,

Concerned file : sample.txt
Operation : i open the file in read mode and don't close the file.
remove() returns -1 and the file is not deleted.

The above operation is successful when i close the file just before
the delete. Does having a file handle to a file impact remove() ? Is
this documented somewhere?

Code sample : [...]
p = fopen("c:\\sam.txt","r"); [...]
printf("result of deleteion is : %d\n",remove("c:\\sam.txt"));
[...]

What happens when you use remove() on an open file is implementation-
defined.

Windows will not let you delete a file that is open.

<OT>
Note that you may be able to use the Windows-specific GetLastError()
to determine _why_ remove() failed. Check your manual or ask in a
Windows-specific group on how to use GetLastError() and how to
translate the number into an error message. I would suspect that
your program would give you some sort of "file in use" error.
</OT>

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top