How to determine whether file is in use and prompt an error message in VC++?
Hello All,
I am using GetSaveFileName function to create Save dialog box and to save specified file in VC++.
But I want to detect, whether the specified file is already open or in use before save.
How to determine whether file is in use and prompt an error message?
Re: How to determine whether file is in use and prompt an error message in VC++?
Originally Posted by anish2006
When user try to save file by click on save button from Save dialog box.
File is NOT been truing to save when user "clicks on save button from Save dialog box". The only thing the user obtains from this dialog is the path name of the file to save to.
You can use OFN_OVERWRITEPROMPT flag to to generate a message box if the selected file already exists
Re: How to determine whether file is in use and prompt an error message in VC++?
Hello Victor,
Thanks for the response. I understood your point here.
Actully I am using OFN_OVERWRITEPROMPT flag to generate a message box for file already exist.
My actual requirement is to display 2 messages to resolve below purpose->
Code:
<1> If file already exist(that has been resolved by using OFN_OVERWRITEPROMPT flag)
<2> If file is already open or in use and user is trying to save the same file.
After user clicks on 'OK' button of "File already exist" dialog box then I am not aware about point <2> how to display error message for "File is already open or in use".
I hope above explanation gives better idea about my actual requirement.
Re: How to determine whether file is in use and prompt an error message in VC++?
Well, in the case of <2> you'll get some error code/message from Windows or an API used by you to save to this file (something like "access denied" or similar)
Re: How to determine whether file is in use and prompt an error message in VC++?
Thanks for your idea. I got the solution.
Please find below the basic code.
Code:
OPENFILENAME opnFile;
FILE *fp;
ZeroMemory( &opnFile, sizeof(OPENFILENAME) );
openFile.lStructSize = sizeof(OPENFILENAME);
opnFile.hwndOwner = hWnd;
opnFile.lpstrFile = szFile; //declare actual parameter as per requirement
openFile.Flags = OFN_OVERWRITEPROMPT;
if ( GetSaveFileName( &openFile ) == TRUE )
{
fp = fopen(opnFile.lpstrFile,"w");
if(fp!=NULL)
{
//do some stuff
}
else
{
// display error for file is in use
}
}
The file pointer returns null, when specified file is in use.
The program displays message in below order->
<1> First it will display "FileSave" dialog box
<2> When user clicks on save button, it displays "File overwrite" warning dialog box
<3> When user clicks on "OK" button, it displays error message "File is in use".
Bookmarks