Delete a file that's in use

This is a discussion on Delete a file that's in use within the C++ Programming forums, part of the General Programming Boards category; I know it can be done, somehow you have to close any handle that has that file open in other ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question Delete a file that's in use

    I know it can be done, somehow you have to close any handle that has that file open in other programs. The problem is HOW?

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Is this for console prog or Windows prog? o_O

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    You cannot force other programs to close a handle to a file. You can just try to kill them - and the user will probably kill YOU if you do that.

    Of course, not all OSs prevent deletion of a file that is open. Linux (and most other *nixes, I think) let you allow any file at any time, provided you have the permissions. Unless you explicitly use a locking mechanism to lock the file.

    So this thread should be moved to the Windows board.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    11
    try to unlink() it. It should work.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    DEV-C++ on WIN2000
    Windows doesn't allow you to delete a file that's already open. Try this:
    Code:
    #include <iostream>
    #include <cstdio>
    
    int main(int argc, char *argv[]) {
        if(std::remove(argv[0])) {
            std::cout << "Error deleting self" << std::endl;
        }
    
        return 0;
    }
    On Windows, you can't delete a file that's already open/running.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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

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