Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Checking whether a file is already in use or not.

Posted on 2003-12-09
5
Medium Priority
?
220 Views
Last Modified: 2013-11-20
Hi,

I am trying to write a rotuine, which will check whether a given file is already in use or not.

For writing this, I am using the routine, OpenFile with OF_SHARE_EXCLUSIVE mode.

When OpenFile is used on a file which is already opened by any other application, then it is returning error message (as it should) which is indicating that the file is already in use.

This seems to work fine only with Binary files.

when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode. Even from windows explorer also, the text based files opened in any other application can be deleted or modified without any problem.

Can someone tell me a way of checking both Text Based files and Binary files which are already in use ?

Thanks!
Gangisetti

0
Comment
Question by:gangisetti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 9

Accepted Solution

by:
Priyesh earned 400 total points
ID: 9909972
Try this:

      HANDLE hFile = CreateFile("c:\\1.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL) ;
      if(hFile == INVALID_HANDLE_VALUE)
      {
            LPVOID lpMsgBuf;
            if (!FormatMessage(
                  FORMAT_MESSAGE_ALLOCATE_BUFFER |
                  FORMAT_MESSAGE_FROM_SYSTEM |
                  FORMAT_MESSAGE_IGNORE_INSERTS,
                  NULL,
                  GetLastError(),
                  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                  (LPTSTR) &lpMsgBuf,
                  0,
                  NULL ))
            {
               // Handle the error.
               return TRUE ;
            }

            // Process any inserts in lpMsgBuf.
            // ...

            // Display the string.
            AfxMessageBox((LPCTSTR)lpMsgBuf) ;

            // Free the buffer.
            LocalFree( lpMsgBuf ) ;
      }

If any open handles exists to the above file, there will be an OS error.  This is true for all files. Editors might just read the file into memory and then close handle, that's why you could delete the file even when it is open in an editor.

If this does not work for you, you can look into file/ directory watch (NT and higher)
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9910039
>> when a text based file ( with extensions .txt, .cpp, .c and etc) , is opened by another applicaiton (for e.g. if file.c is opened in MSDEV) OpenFile is still able to get access to the file in OF_SHARE_EXCLUSIVE mode.

This happens because Visual Studio doesn't keep opened file handle. It opens file only to load it into memory and to save it. So, your program is OK.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question