473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deleting a File

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_

Apr 23 '07 #1
10 1159
In article <11*********************@y80g2000hsf.googlegroups. com>,
<kv******@hotmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Apr 23 '07 #2
kv******@hotmail.com wrote, On 23/04/07 22:58:
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.
--
Flash Gordon
Apr 23 '07 #3
kv******@hotmail.com writes:
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");
--
"IMO, Perl is an excellent language to break your teeth on"
--Micah Cowan
Apr 23 '07 #4
kv******@hotmail.com wrote:
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;
}
Apr 23 '07 #5
ri*****@cogsci.ed.ac.uk (Richard Tobin) wrote:
In article <11*********************@y80g2000hsf.googlegroups. com>,
<kv******@hotmail.comwrote:
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:
But I'm wondering, is there a better way to do this
in C?

Yes, the remove() function.
And, surprise, surprise, this is in the FAQ:
<http://c-faq.com/osdep/delete.html>.

Richard
Apr 24 '07 #6
Martin Ambuhl wrote:
kv******@hotmail.com wrote:
>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;
}
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;
}

:-)

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline.net
--
Posted via a free Usenet account from http://www.teranews.com

Apr 24 '07 #7
Thanks! These replies were very helpful.

Apr 24 '07 #8
CBFalconer <cb********@yahoo.comwrote in
news:46***************@yahoo.com:
>
#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;
}
Apr 24 '07 #9
In article <46***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>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...

--
Prototypes are supertypes of their clones. -- maplesoft
Apr 24 '07 #10
zero wrote, On 24/04/07 19:11:
CBFalconer <cb********@yahoo.comwrote in
news:46***************@yahoo.com:
>#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?
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 :-)
--
Flash Gordon
Apr 24 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
4
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
2
by: GMK | last post by:
Dear all in my asp.net application i have a text file that is installed with my application on the server. this text file is filled with data through a web interface in my application. i need to...
5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
2
by: SiouxieQ | last post by:
Hi there, I'm using the code below to try to delete a name from a list of names in a file. Unfortunately it doesn't quite do what I want it to. Instead of looking for the name in the...
5
by: Sandeep Singh Sekhon | last post by:
I am Developing a Web Application with ASP.NET 1.1 I have one project Folder which is virtual directory of IIS. In this directory, I have one Folder named Photos in which I used to Store Photos....
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
13
by: programming | last post by:
how do i delete from a text file 1 of the following lines: jon|scott adam|smith <--delete paul|clark say i would like to delete the middle line of this txt, in member.txt what php code or...
1
by: diyasher | last post by:
hello my code is in c#. i am using fileSystemWatcher class to watch event when file is deleted. event is fire when file is deleted, i want to stop deleting file, means when user want to delete...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.