Thread: check file opened or not?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    check file opened or not?

    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?

    C++ is fine.


    thanks in advance,
    George

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can try to open the file with "NO sharing", then it will fail if some other process has the file open.

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

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    You mean ::OpenFile() with OF_SHARE_EXCLUSIVE?

    Quote Originally Posted by matsp View Post
    You can try to open the file with "NO sharing", then it will fail if some other process has the file open.

    --
    Mats

    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ifstream is fine.
    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. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    You mean filebuf::sh_none?

    Quote Originally Posted by Elysia View Post
    ifstream is fine.

    regards,
    George

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ifstream::open with appropriate open protection constants, which I don't know what they're called or located.
    Last edited by Elysia; 04-03-2008 at 07:18 AM.
    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. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    when you say a file do you mean a process or what , because your making it sound like a .txt file

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Anddos View Post
    when you say a file do you mean a process or what , because your making it sound like a .txt file
    I think George2 is asking about files like .txt and such.

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

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


    What do you mean "what they're called or located"?

    Quote Originally Posted by Elysia View Post
    which I don't know what they're called or located.

    Thanks Anddos,


    I mean data files, like .txt or something. Any ideas to my original question?

    Quote Originally Posted by Anddos View Post
    when you say a file do you mean a process or what , because your making it sound like a .txt file

    regards,
    George

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


    Right Mats!

    Quote Originally Posted by matsp View Post
    I think George2 is asking about files like .txt and such.

    --
    Mats

    regards,
    George

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Thanks Elysia,
    What do you mean "what they're called or located"?
    I mean I don't know how to call ifstream with appropriate arguments.
    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. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    So you mean you are not sure whether using filebuf::sh_none is correct?

    Quote Originally Posted by Elysia View Post
    I mean I don't know how to call ifstream with appropriate arguments.

    regards,
    George

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know, but I doubt it. The class takes its arguments from ios_base.
    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. #14
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    and to check if its open ?
    i would check if the process notepad is open then use FindWindow to check is the string of the windowtext is matching your textfile description

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    George wants to find out if the file is open, not if a process is open. And this doesn't just include Notepad. What you describe is a really horrible way of doing 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.

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