CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2010
    Posts
    15

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

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: How to determine whether file is in use and prompt an error message in VC++?

    As a result of which user action?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    Nov 2010
    Posts
    15

    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.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How to determine whether file is in use and prompt an error message in VC++?

    Quote Originally Posted by anish2006 View Post
    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

  5. #5
    Join Date
    Nov 2010
    Posts
    15

    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.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  7. #7
    Join Date
    Nov 2010
    Posts
    15

    Red face 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
  •  





Click Here to Expand Forum to Full Width

Featured