Thread: how do you check if a file ( a.txt ) is being used by another process?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    how do you check if a file ( a.txt ) is being used by another process?

    Code:
    int main(int argc,char *argv[])
    {
        if ( no Being used by another process )
                      remove("a.txt");
               
         return 0;
    }
    how do you check if a file ( a.txt ) is being used by another process?
    thanks.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    It depends on your OS.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by King Mir View Post
    It depends on your OS.
    My OS is read hat (linux OS), how to solve this problem?

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    use lsof ./a.txt. This will show you not only how many processes of a file open but who they are. Hint: this also works on sockets too
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by jeffcobb View Post
    use lsof ./a.txt. This will show you not only how many processes of a file open but who they are. Hint: this also works on sockets too
    Your answer is my want, thans a lot !
    moreover if my OS is windows, how to check?
    In windows OS,there is not lsof command.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by zcrself View Post
    Your answer is my want, thans a lot !
    moreover if my OS is windows, how to check?
    In windows OS,there is not lsof command.
    Don't ask me; I only code for Windows if there is a gun pointing at my head...all kidding aside there are a lot of Redmond-ites here that I am sure would be happy to tell you..cool thing about NIX though is you can get the source code to lsof and code it right into your app
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by jeffcobb View Post
    Don't ask me; I only code for Windows if there is a gun pointing at my head...all kidding aside there are a lot of Redmond-ites here that I am sure would be happy to tell you..cool thing about NIX though is you can get the source code to lsof and code it right into your app

    Your are very amusing and very humorous !

    thank you all the same

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-20-2009, 12:46 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM