?
Solved

How do I delete a file?

Posted on 2011-03-08
7
Medium Priority
?
331 Views
Last Modified: 2013-12-14
Ok, every time I run my little application it leaves behind a text file called C:\debug.txt.

I would like my app to delete this.  

What command/code do I need to use in VC++ to delete this file?  

Thank you!
0
Comment
Question by:shaolinfunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 35068506
One approach:
http://www.cplusplus.com/reference/clibrary/cstdio/remove/
/* remove example: remove myfile.txt */
#include <stdio.h>

int main ()
{
  if( remove( "myfile.txt" ) != 0 )
    perror( "Error deleting file" );
  else
    puts( "File successfully deleted" );
  return 0;
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35068518
For full path, you may need \\ as in:
remove("c:\\debug.txt");

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35068548
Oh, I forgot to mention that you should close your file before deleting it.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 35068575
Works like a charm.  Thanks phoffric!
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35068578
You didn't mention how you open and close, but in case you need this, here is sample code from:
     http://www.cplusplus.com/reference/iostream/ofstream/close/
     // ofstream::open
#include <fstream>
using namespace std;

int main () {

  ofstream outfile;

  outfile.open ("test.txt", ofstream::out | ofstream::app);
  outfile << "This sentence is appended to the file content\n";
  outfile.close();

  return 0;
}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 35068583
Anytime. :)
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35068591
yes, thank you for that additional info...i closed the file by using:

outfile.close();
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

770 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