Solved

How do I delete a file?

Posted on 2011-03-08
7
315 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
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
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.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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