[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I delete a file?

Posted on 2011-03-08
7
Medium Priority
?
336 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
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 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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

656 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