Thread: Deleting contents of already opened file?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    13

    Question Deleting contents of already opened file?

    Is there anyway to delete (truncate) the contents of a file that's already attached to a stream object, like fstream or ifstream? I could do it by closing the file and reopening it with ios::trunc specified, but I'd rather not close the file as there's a good chance another instance of the application might grab it since the file would become unlocked when it was closed...

    I'm really desperate for help, so if you have any ideas at all, please share them.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    one possible solution is to copy the contents of the file to a temporary file specifying ios::trunc then remove the first file and rename your new file to the name of the first one.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    I'd thought of that, but there's still the problem of file-locking. This file is a sorted file containing structures (representing IP addresses). This program is a web counter written in C++. Since there could very well be multiple instances of the counter running at once, each instance locks the file while its searching/inserting/deleting entries. If an instance of the program can't get access to the file, it sleeps for a few ms, then tries again until it gets access. The problem with deleting the file is that the file will no longer be locked, and another instance of the program could recreate the file and locking, causing some (or all) of the IP's to be lost. At the very best, that one instance of the counter would add an IP to the blank file, which would be overwritten when the other instance of the program copies the entries back in.

    It would be nice if there was an fstream method that could truncate a file, but there isn't, and it doesn't look like there is a function that can do it either...

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    What you could do is use the "get" and "put" file pointers. Depends on what you are using <stdio.h> or <fstream> the functions vary (for iostream I think they are seekg(), seekp(), tellg() and tellp()). But the idea is as follows:

    When you want to delete the contents of the file you move the pointer to the beginning of the file and then either overwrite the contents of the file with spaces or zeroes or something, or just start writing to the file from the beginning.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Or, use a lockfile (works on any OS) or something like a mutex in win32 to synch your access to the file. Instead of trying to lock the file itself, the app with control will create a file, the lock file, and as long as this file exists no other instance can access the file. Then, you can close, reopen, and do whatever you like.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    13
    That's a good idea, I hadn't thought about that...

    I think I'm going to go a different route for this counter though. The script was going to truncate (clear out) the IP log file every day. Instead, I'm going to optimize the IP storage strucutre a bit, add a time_t field to it, and store the time that the IP was added/updated. Whenever the counter registers a hit, it will scan the file for that IP. If it finds it, it'll check to see how long its been since the last counted hit. If it's been more than 24 hours, it will count the hit as unique and update that IP strucutre (which will only affect that entry in the file). That's probably a better way to do it anyway, as it will allow for counting total uniques and other things in the future.

    Still, it would be soooo much simpler if there was a function that could truncate a file while it's open... I can't count the number of times I've wished for that...

    ---Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. C Delete opened file
    By gidion in forum C Programming
    Replies: 25
    Last Post: 12-04-2008, 10:35 AM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM