Thread: DeleteFile() Function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    42

    DeleteFile() Function

    Hi,

    when i try to delete a file with DeleteFile function it returns 0 and the error code is 32 ( ERROR_SHARING_VIOLATION ).

    Can you tell me how i can force the Deletion of the file?I know that we can mark it for deletion on restart( MoveFileEx function ), but is it there another way to do it?

    Can we get a list of the file's opened handles, and then close them all and finally delete the file? If yes, what is the function to get the file's opened handles?


    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Is this file opened by your process or by the other process?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    The file is opened by other process.

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    If the file doesn't need to be deleted, you could try opening the file in write share mode and just wipe the file's content.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Maybe truncating it would be a better choice, by setting the file's length. I don't remember the function for that, however.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by Elysia View Post
    Maybe truncating it would be a better choice, by setting the file's length. I don't remember the function for that, however.
    By setting the file's length to what? Do you mean the ios::trunc function?

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    O.K i have managed to wipe the file with zero's as Yarin said.

    Under Windows Xp i have no problems, but under Windows vista i get this message:

    Can not enumerate files in directory 'C:\Users\Administrator\Local Settings\Temporary Internet Files' (error 5: Access denied)
    Can you tell me please, how can i grant the required permission Under Windows Vista to access the file( programmatically ) ?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Set your manifest to require admin privileges to run it. This is just an option in compiler settings if you use Visual Studio 2008.
    You can also truncate your file by using windows SetEndOfFile API.
    Seek to beginning and call it, and your file's contents will be truncated and filesize will be 0. Pretty easy way to delete it.
    Last edited by Elysia; 03-27-2008 at 03:33 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If the file is in use by another process, is it really a good idea to delete or truncate it? It could cause problems for the other process.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Typically it's the same, though.
    If another process uses it, it will usually be locked, so you can neither open nor delete it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by patrick22 View Post
    Can we get a list of the file's opened handles, and then close them all and finally delete the file? If yes, what is the function to get the file's opened handles?


    Thanks in advance.
    SysInternals' Process Explorer can show you file handles and also close the handle of a currently open file. www.sysinternals.com redirects to a MS web-page that has the process explorer and some other useful tools.

    This is OBVIOUSLY not a good thing to do in general, but I have used to to get past a locked-up application when trying to avoid restarting the machine just because some app has locked up [with some file I need to work on, or a lock-file that needs to be released to start another instance].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I tend to use Unlocker for wrestling locked files from applications. Handy thing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    Quote Originally Posted by Elysia View Post
    Set your manifest to require admin privileges to run it. This is just an option in compiler settings if you use Visual Studio 2008.
    Actually, i am not using Visual Studio 2008. Visual Studio 2005 Express Editon is good for me.

    Quote Originally Posted by Elysia View Post
    You can also truncate your file by using windows SetEndOfFile API.Seek to beginning and call it
    Wow Elysia, i will give it a try.

    Quote Originally Posted by cpjust
    If the file is in use by another process, is it really a good idea to delete or truncate it? It could cause problems for the other process.
    It depends on, which file we are trying to delete, and on what we are trying to achieve?

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    42
    I have tried the SetEndOfFile function, it did not work.

    GetLastError() returns the following error:

    ERROR_USER_MAPPED_FILE : The requested operation cannot be performed on a file with a user-mapped section open.

    What does that mean?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM