C Delete opened file

This is a discussion on C Delete opened file within the C Programming forums, part of the General Programming Boards category; If you do 3 then 4, you are opening [1] the file twice and loosing the first handle. So that ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you do 3 then 4, you are opening [1] the file twice and loosing the first handle. So that handle from createfile can only be closed by exiting the application itself - as the handle for it is lost.

    [1] Ok, technically, creating, then opening - but that's two handles opened for the same file - print out the handle after you've called CreateFile in each case.

    --
    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.

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    ahhh,
    Didn't realise it was possible to have 2 handles open for the same file. Or even that the create process leaves it in an open state.

    Will look into how to print the handle (as output is all different from what i'm used to in Java).
    Will also see about placing a check on the open option to check if the file handle is already open.

    Thanks matsp!!

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm pretty sure that in 32-bit Windows, a handle is a 32-bit Unsigned, so printf using %u should allow you to print a handle [actually, in general, a handle is actually the address of the kernel data structure representing the resource that the handle represents - makes it easy to access for the kernel].

    Yes, createfile, whether you do CREATE_NEW or OPEN_EXISTING will "open" the file.

    --
    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.

  4. #19
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    			case 3 :
    				//create option
    				hFile = CreateFile(fname,               // file to be opened
    					GENERIC_WRITE,      // open for writing
    					FILE_SHARE_DELETE | FILE_SHARE_WRITE,   // share for writing					
                                            NULL,               // default security
    					CREATE_NEW,         // create new file only
    					FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
    					// normal file archive and impersonate client
    					NULL);              // no attribute template

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BobS0327 View Post
    Code:
    			case 3 :
    				//create option
    				hFile = CreateFile(fname,               // file to be opened
    					GENERIC_WRITE,      // open for writing
    					FILE_SHARE_DELETE | FILE_SHARE_WRITE,   // share for writing					
                                            NULL,               // default security
    					CREATE_NEW,         // create new file only
    					FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
    					// normal file archive and impersonate client
    					NULL);              // no attribute template
    That will only allow the file to be opened by another (or the same) process for reading/writing - not allow someone to delete a file that is held open, and since the first handle is lost when the second step of the reproduction is done, it's not EVER going to be closed (unless we exit the app).

    --
    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.

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by zacs7 View Post
    > If you can write to open files, it can even be a security risk.
    That's what flockfile() is for... Bang supposed security risk gone (I can't even see how there would be one anyway...)
    Oops. I misworded. I did not mean write to file, but actually delete or truncate an open file.
    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.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Oops. I misworded. I did not mean write to file, but actually delete or truncate an open file.
    But you are not deleting or truncating an open file - you are just removing the directory entry for it. And you can only do that on files that are:
    - Not locked.
    - Available for you to modify/remove anyways.

    It is not a security hole, because any files that affect the system security, assuming the system is intended to be secure anyways, are not accessible by non-trusted users. If trusted users are sabotaging the system they are trusted to use - then you have misapplied trust, and it usually is not something you are allowed to do according to the employment contract - that is, you get the sack if it's intentional, and a stern warning if it was a mistake.

    --
    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.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    But the point was that being able to delete a file that is open by a process can have bad effects. But it seems that no such thing can be done, but there was a claim that it could be, to which I responded it seemed strange.
    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. #24
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Quote Originally Posted by matsp View Post
    That will only allow the file to be opened by another (or the same) process for reading/writing - not allow someone to delete a file that is held open, and since the first handle is lost when the second step of the reproduction is done, it's not EVER going to be closed (unless we exit the app).

    --
    Mats
    I think that's more relevent to his menuing system. For instance, if the file does not exist, then option 4 (open file) should not be available. Thus, requiring the user to select option 3 (create file). On the other hand, if the file exists, Option 4 (open file) should be available and Option 3 (create file) should not be available.

    If the user tries to open the file prior to creating the file, the app will indicate that it cannot open the file. But the OP poster doesn't check to verify that the file is already open in the Open file #4 option. So, the OP should either do more error checking or make the menu system more user friendly.

    The end user may have to rethink how he's using CreateFile. But anyway, no matter how he uses CreateFile, he'll still need the additional attribute.

    He may want to enhance his menuing system to only allow relevant options. For instance, assuming that the OP poster starts using an array of file handles, if all files are closed, there is no reason to give the end user the option to close the file. Same thing for deleting the files. The menuing system should provide a means of guidance to the end user for making relevant decisions.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    But the point was that being able to delete a file that is open by a process can have bad effects. But it seems that no such thing can be done, but there was a claim that it could be, to which I responded it seemed strange.
    It can be in the sense that the directory entry goes away - you can even create a new file to replace the previous file (e.g. copy a file onto the same name as an existing open one). But only if you have the right permissions and the file is not locked, of course.

    Further, the file itself still stays on the disk (but no longer available to open again). It is different from Windows, where opening a file normally prevents all others from doing anything to that file - which means for example that you can't replace an existing executable with another executable or replace a script-file that some executable is using, without shutting down the executable.

    --
    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.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by BobS0327 View Post
    I think that's more relevent to his menuing system. For instance, if the file does not exist, then option 4 (open file) should not be available. Thus, requiring the user to select option 3 (create file). On the other hand, if the file exists, Option 4 (open file) should be available and Option 3 (create file) should not be available.
    Yes, of course, a clever menu system that only allows you to do things that are meaningful is definitely a good idea. However, I think the OP didn't understand the API itself - which was the original problem - the file is opened twice by the application, and the handle from the first open (create) is lost, thus it can't be closed.

    --
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM

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