?
Solved

How do I delete a file?

Posted on 2011-03-08
7
Medium Priority
?
337 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

864 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