Deleting a File

K

kvnsmnsn

I'm at a point in my code where I need to delete a file. For right
now I'm going to use the <system()> function like so:

system( "rm Abc.Txt");

which will work. But I'm wondering, is there a better way to do this
in C? Any information on this would be greatly appreciated.

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
R

Richard Tobin

I'm at a point in my code where I need to delete a file. For right
now I'm going to use the <system()> function like so:

system( "rm Abc.Txt");

which will work.

Well, it will work on systems where the delete is "rm".
But I'm wondering, is there a better way to do this
in C?

Yes, the remove() function.

-- Richard
 
B

Ben Pfaff

I'm at a point in my code where I need to delete a file. For right
now I'm going to use the <system()> function like so:

system( "rm Abc.Txt");

which will work. But I'm wondering, is there a better way to do this
in C? Any information on this would be greatly appreciated.

#include <stdio.h>

remove ("Abc.Txt");
 
F

Flash Gordon

I'm at a point in my code where I need to delete a file. For right
now I'm going to use the <system()> function like so:

system( "rm Abc.Txt");

which will work. But I'm wondering, is there a better way to do this
in C? Any information on this would be greatly appreciated.

remove("Abc.Txt");

You should check the value returned to see if it succeeded.
 
M

Martin Ambuhl

I'm at a point in my code where I need to delete a file. For right
now I'm going to use the <system()> function like so:

system( "rm Abc.Txt");

which will work. But I'm wondering, is there a better way to do this
in C? Any information on this would be greatly appreciated.

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

int main(void)
{
return remove("Abc.Txt") ? EXIT_FAILURE : 0;
}
 
C

CBFalconer

Martin said:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
return remove("Abc.Txt") ? EXIT_FAILURE : 0;
}

We can improve this:

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

int main(int argc, char **argv)
{ int result;

result = 0;
while (argc > 1) {
shift(); /* questionable */
if (remove(argv[0])) result = EXIT_FAILURE;
}
return result;
}

:)
 
Z

zero

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

int main(int argc, char **argv)
{ int result;

result = 0;
while (argc > 1) {
shift(); /* questionable */

What does this do, and why is it questionable?
if (remove(argv[0])) result = EXIT_FAILURE;
}
return result;
}

Isn't argv[0] the executable? I would expect something like this:

while( ++argv )
{
if ( remove( *argv ) && !result )
result = EXIT_FAILURE;
}
 
F

Flash Gordon

zero wrote, On 24/04/07 19:11:
What does this do, and why is it questionable?

It is a function that Chuck did not provide, probably deliberately.
if (remove(argv[0])) result = EXIT_FAILURE;
}
return result;
}

Isn't argv[0] the executable?

I suspect that was deliberate as well.
> I would expect something like this:

while( ++argv )
{
if ( remove( *argv ) && !result )
result = EXIT_FAILURE;
}

That would not be anywhere near as much fun :)
 
W

Walter Roberson

We can improve this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{ int result;

result = 0;
while (argc > 1) {
shift(); /* questionable */
if (remove(argv[0])) result = EXIT_FAILURE;
}
return result;
}

It appears to me that you are expecting the undefined routine
shift() to move argv to the next argv value, decrementing argc.
Or at least shift() is not defined by C89...
 

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
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top