Search code examples
visual-c++opencascade

Check a file already exists in Save As Dialog box - c++, opencascade


I want to create a .stl file for a particular shape where each face of that shape has a different patch name like face1,face 2 etc. I have done this by overriding the StlAPI_Writer and RWStl classes in opencascade. I have used file.Append method instead of file.Build method to do so. But I have a problem when I save the .stl file in an already existing file, it appends data to the existing one which is incorrect. I want to delete the existing data in the file and append new data face by face for a given shape.

Please help me on this.


Solution

  • You can use this simple function:

    #include <sys/stat.h>
    #include <string>
    
    using namespace std;    
    
    bool FileExists(string strFilename) {
      struct stat stFileInfo;
      bool blnReturn;
      int intStat;
    
      // Attempt to get the file attributes
      intStat = stat(strFilename.c_str(),&stFileInfo);
      if(intStat == 0) {
        // We were able to get the file attributes
        // so the file obviously exists.
        blnReturn = true;
      } else {
        // We were not able to get the file attributes.
        // This may mean that we don't have permission to
        // access the folder which contains this file. If you
        // need to do that level of checking, lookup the
        // return values of stat which will give you
        // more details on why stat failed.
        blnReturn = false;
      }
    
      return(blnReturn);
    }
    

    I assume you use the SaveFileDialogue class. In this case you can handle the return result of the dialogue like this:

      if ( saveFileDialog.ShowDialog() == ::DialogResult::OK )  {
         if ( FileExist(saveFileDialog.FileName) )  {
            // erase the file
         }
         // write the code using the Append function
      }
    

    This should work, however a easier variant must be accessible if you use something else than Append (something like Write or maybe even Append but with a parameter that specifies to rewrite the file)

    HTH, JP