Erasing the contents of a file?

This is a discussion on Erasing the contents of a file? within the C++ Programming forums, part of the General Programming Boards category; What is the quickest way to erase the contents of an fstream? I don't want to delete the file, just ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    13

    Erasing the contents of a file?

    What is the quickest way to erase the contents of an fstream? I don't want to delete the file, just make it blank.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    The simplest way is to open the file for writing. This should automatically clear all previous data in the file and set the position pointer to 0.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    So the following line will erase the contents of a.txt?

    Code:
    fstream target( "a.txt", ios :: out );

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Also, is there any easy way to get the name of the file which the fstream was opened on? That is, given an fstream, can you give me the name of the file the fstream is "pointing to"?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >So the following line will erase the contents of a.txt?
    Yes

    >is there any easy way to get the name of the file which the fstream was opened on?
    Nothing portable that I can think of off-hand except for having a string which holds the name of the file.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    13
    Thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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