Thread: check file opened or not?

  1. #16
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    no he said he wants to check if a file is open ,
    by other thread or process or even the same thread
    you need to learn to read dude!!!!!!!!!!!

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And where does that say "notepad"?
    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.

  3. #18
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    notepad is a process you nubcake , if he wants to know if a txt file is open then this is the way !!!!!!!!
    i will write the code out for you later on seeing as i am a nice guy
    Last edited by Anddos; 04-03-2008 at 08:18 AM.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Notepad is one process. A file is a file and can be held open by one or more processes, not limited to notepad.
    This is NOT the way.
    Opening the file in exclusive mode is an easy way to know if another process is already holding the file open or not.

    You need to learn to be less rude.
    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.

  5. #20
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    he said a txt file , so the default editor to open a .txt is notepad ,
    the method you said means to open the file not check if the txt file is running !!! jesus christ

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I will repeat the original message:

    Quote Originally Posted by George2 View Post
    Hello everyone,

    On Windows platform, which API could be used to check whether a file is opened (either by other thread or process or even the same thread) or not?
    In other words: how can I figure out if another process (or another thread) is holding the file open?
    Answer: open in exclusive mode. This will fail unless the file is not opened by any other process.
    If it succeeds, then the file is not open by any other process.

    And even though notepad may be the default, it doesn't mean it's the only process that can hold a file open! George did mention "another thread," too, which could mean another thread in the same process perhaps? That's not notepad.
    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
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Anddos, chill out and stop attacking Elysia. There is no reason to inflame an otherwise calm thread.

    Windows, AFAIK, has no command that will tell you which file handles belong to which threads. There are several undocumented NT_ functions within the kernel code that you can call but since they are undocumented and also subject to change I don't know if I would rely on these. I researched this one time to write a utility that would allow me to view which process had a file open and if need be attempt to gain ownership of that handle and close the file. This would eliminate the infamous 'Access denied. ..' error that really only helps viruses and spyware continue to run unmolested on your system.

    There is a website that keeps a database of undocumented Win32 API functions that you can google for and that may assist you. I know it is possible to do this since there are utils out there that can tell you this information.

    ifstream will only tell you about the process in which it is used. For system wide information about which file handles are being used and by whom you would have to make a call into the Windows kernel. There is no way the C/C++ standard libraries will know anything about other tasks or processes in the system that may have files open.
    Last edited by VirtualAce; 04-03-2008 at 07:10 PM.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Bubba,


    Two more comments,

    1.

    Do you mean there are some methods from ifstream class, which can tell which handle is used by which process? I have performed some research, but failed.

    Could you indicate which method from ifstream do you mean from which we can know which process opens the file please? :-)

    2.

    Quote Originally Posted by Bubba View Post
    Windows, AFAIK, has no command that will tell you which file handles belong to which threads. There are several undocumented NT_ functions within the kernel code that you can call but since they are undocumented and also subject to change I don't know if I would rely on these. I researched this one time to write a utility that would allow me to view which process had a file open and if need be attempt to gain ownership of that handle and close the file. This would eliminate the infamous 'Access denied. ..' error that really only helps viruses and spyware continue to run unmolested on your system.
    You mean there is public API in ifstream which can tell which process opens the file, not there is no open API to tell which thread opens the file?


    Thanks Elysia,


    You correctly understand my points. To be honest, my English is not very good, but you can understand all of my points. Appreciated. :-)

    (Or, it means you have the same level of English skills -- just joking.)

    Quote Originally Posted by Elysia View Post
    I will repeat the original message:



    In other words: how can I figure out if another process (or another thread) is holding the file open?
    Answer: open in exclusive mode. This will fail unless the file is not opened by any other process.
    If it succeeds, then the file is not open by any other process.

    And even though notepad may be the default, it doesn't mean it's the only process that can hold a file open! George did mention "another thread," too, which could mean another thread in the same process perhaps? That's not notepad.

    regards,
    George

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Do you mean there are some methods from ifstream class, which can tell which handle is used by which process? I have performed some research, but failed.

    Could you indicate which method from ifstream do you mean from which we can know which process opens the file please? :-)

    You mean there is public API in ifstream which can tell which process opens the file, not there is no open API to tell which thread opens the file?
    No, that's not right. There is no method in ifstream to tell you that. There are undocumented Win32 API that does, however.

    Have a look at http://undocumented.ntinternals.net/
    Just beware of using these functions. They are usually undocumented for a reason.
    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.

  10. #25
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Looks like Bubba is more familiar with ifstream class. :-)

    Let me wait for him to see whether option filebuf::sh_none is the one for my question.

    Quote Originally Posted by Elysia View Post
    No, that's not right. There is no method in ifstream to tell you that. There are undocumented Win32 API that does, however.

    Have a look at http://undocumented.ntinternals.net/
    Just beware of using these functions. They are usually undocumented for a reason.

    regards,
    George

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Bubba explicitly told you that ifstream doesn't work:

    Quote Originally Posted by Bubba View Post
    Anddos, chill out and stop attacking Elysia. There is no reason to inflame an otherwise calm thread.

    Windows, AFAIK, has no command that will tell you which file handles belong to which threads. There are several undocumented NT_ functions within the kernel code that you can call but since they are undocumented and also subject to change I don't know if I would rely on these. I researched this one time to write a utility that would allow me to view which process had a file open and if need be attempt to gain ownership of that handle and close the file. This would eliminate the infamous 'Access denied. ..' error that really only helps viruses and spyware continue to run unmolested on your system.

    There is a website that keeps a database of undocumented Win32 API functions that you can google for and that may assist you. I know it is possible to do this since there are utils out there that can tell you this information.

    ifstream will only tell you about the process in which it is used. For system wide information about which file handles are being used and by whom you would have to make a call into the Windows kernel. There is no way the C/C++ standard libraries will know anything about other tasks or processes in the system that may have files open.
    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.

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    To my requirement, I only need to know whether the file is opened by other process/thread or not, no need to know the exact process/thread ID.

    So, I think filebuf::sh_none should be fine?

    Quote Originally Posted by Elysia View Post
    Bubba explicitly told you that ifstream doesn't work:

    regards,
    George

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    An yes, found it. filebuf::sh_none will open the file in exclusive mode, so it should work to tell you if the file is open or not.
    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.

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Question answered.

    Quote Originally Posted by Elysia View Post
    An yes, found it. filebuf::sh_none will open the file in exclusive mode, so it should work to tell you if the file is open or not.

    regards,
    George

  15. #30
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I don't think using ifstream is the right approach. Windows would be the only one who could really tell you if the file was opened by a process. Just because a file is opened in one process does not mean that it is in exclusive mode. This is actually relying on a side effect of what Windows may do to get ifstream to report the information you want. In essence you are using ifstream to report something that it is not responsible for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 08:33 AM