在“另存为”对话框中检查文件是否已存在 - c ++,opencascade

时间:2011-07-15 08:52:15

标签: visual-c++ opencascade

我想为特定形状创建一个.stl文件,其中该形状的每个面具有不同的补丁名称,如face1,face 2等。我通过覆盖opencascade中的StlAPI_Writer和RWStl类来完成此操作。我使用file.Append方法而不是file.Build方法来做到这一点。 但是当我将.stl文件保存在现有文件中时,我遇到了问题,它将数据附加到现有的不正确的文件中。我想删除文件中的现有数据,并为给定的形状逐面添加新数据。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

您可以使用this简单功能:

#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);
}

我假设你使用SaveFileDialogue类。在这种情况下,您可以像这样处理对话的返回结果:

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

这应该可行,但是如果你使用除Append之外的东西(比如Write或者甚至是Append但是带有指定重写文件的参数),则必须可以访问更简单的变体

HTH,JP