how to delete file

Posted on 2000-05-16
Last Modified: 2010-04-02
Here is what my problem is

I am reading stuff form a file, into the program. When it is done i want to delete the file from which i read in. But can't figure out a way of doing this. And need help :)
Question by:Madfun00
  • 4
  • 2
  • 2
  • +1

Expert Comment

ID: 2815710
If this program is always used in Windows you can use the DeleteFile method declared in winbase.h.

BOOL DeleteFile(
  LPCTSTR lpFileName   // pointer to name of file to delete

Author Comment

ID: 2815721
I am sorry, doing it in unix but thank u

Accepted Solution

mournblade earned 100 total points
ID: 2815794
how about a system call?

char filename[] = "myFile.txt";
char system_call[256];
sprintf(system_call, "rm %s", filename);

Author Comment

ID: 2815850
Thanks alot
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!


Author Comment

ID: 2815985
i gave u the points right?? not sure if i did on didn't
LVL 22

Expert Comment

ID: 2816675
That works, but it is not a very efficient way to delete a file.  It requires that a new thread be started for the command processor and the command procssor has to be loaded and initialized in that thread, then the file is deleted, then the command processor terminated and the thread deleted.

You can just use the standard C procedure called remove().  Or you can use the UNIX system call called "unlink()" (I think that is the name.) to delete a file.

Expert Comment

ID: 2817325
madfun00: yes you did.

nietod: i guess you're right. but if this is done only once, it doesn't really matter, does it? i thought that there were C fcns to do the job but i didn't know what they were. thanks for the info.
LVL 22

Expert Comment

ID: 2818825
>> but if this is done only once, it doesn't
>> really matter, does it?
Not too much.  But its a very round-about and innefficient solution.  its like mailing a letter from NY to LA by getting on a plane and going from NY to LA then putting the letter in a mail box 1 block from its destination in LA.

Author Comment

ID: 2818895
Thank u mournblade, and nietod. It is interesting to see how much there is that i still need to know:). And thanks to both of u for the interesting solutions. I am already done and sumited my proj


Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now