-
November 23rd, 2010, 01:57 AM
#1
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?
Thanks.
-
November 23rd, 2010, 02:26 AM
#2
Re: How to determine whether file is in use and prompt an error message in VC++?
As a result of which user action?
-
November 23rd, 2010, 02:53 AM
#3
Re: How to determine whether file is in use and prompt an error message in VC++?
When user try to save file by click on save button from Save dialog box.
Thanks.
-
November 23rd, 2010, 04:48 AM
#4
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
Victor Nijegorodov
-
November 23rd, 2010, 07:49 AM
#5
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.
Thanks.
-
November 23rd, 2010, 07:56 AM
#6
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)
Victor Nijegorodov
-
November 23rd, 2010, 09:48 PM
#7
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".
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|