Solved

How do I delete a file?

Posted on 2011-03-08
7
305 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
  • 5
  • 2
7 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
Comment Utility
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
Comment Utility
For full path, you may need \\ as in:
remove("c:\\debug.txt");

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
Oh, I forgot to mention that you should close your file before deleting it.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Closing Comment

by:shaolinfunk
Comment Utility
Works like a charm.  Thanks phoffric!
0
 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
Comment Utility
Anytime. :)
0
 
LVL 1

Author Comment

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

outfile.close();
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 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

11 Experts available now in Live!

Get 1:1 Help Now