By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,306 Members | 1,621 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,306 IT Pros & Developers. It's quick & easy.

remove() [ to delete a file ] fails

P: n/a
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"));
}

Nov 7 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
sa*****@yahoo.co.in wrote:
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."

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Nov 7 '06 #2

P: n/a
sa*****@yahoo.co.in wrote:
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

Nov 7 '06 #3

P: n/a
>
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???

Nov 7 '06 #4

P: n/a
sa*****@yahoo.co.in said:
>
>>
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...
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 7 '06 #5

P: n/a
Bill Pursell wrote:
sa*****@yahoo.co.in wrote:
>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.

Nov 7 '06 #6

P: n/a
sa*****@yahoo.co.in wrote:
>
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:Th*************@gmail.com>

Nov 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.