Thread: How can I delete a file?

  1. #1

    How can I delete a file?

    I need to open a file using ofstream for appending.

    However I first want to make sure that the file does not exist, and if it does I want to delete all of the data inside.

    How can I do this?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Try this. If the file exists, it will "truncate" the contents of the file after opening it.
       char *filename = "info.txt";
       ofstream out(filename,ios::trunc);
       if (!out)
          cout << "Unable to open file " << filename << endl;
          return 1;
    If you just want to delete a file, use remove(filename) and #include <stdio.h>
    Last edited by swoopy; 11-19-2001 at 04:41 PM.

  3. #3



    Thanks a ton!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM