How can I delete a file?

This is a discussion on How can I delete a file? within the C++ Programming forums, part of the General Programming Boards category; I need to open a file using ofstream for appending. However I first want to make sure that the file ...

  1. #1
    Unregistered
    Guest

    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
    Posts
    2,934
    Try this. If the file exists, it will "truncate" the contents of the file after opening it.
    Code:
       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
    Unregistered
    Guest

    Talking

    perfect,

    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, 11:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 02:19 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21