2011-07-15 58 views
0

我想爲特定形狀創建.stl文件,其中該形狀的每個面具有不同的修補程序名稱,如face1,face 2等。通過覆蓋opencascade中的StlAPI_Writer和RWStl類。我已經使用file.Append方法而不是file.Build方法來執行此操作。 但是,當將.stl文件保存在已存在的文件中時,我遇到問題,它會將數據追加到現有的不正確的文件中。我想要刪除文件中的現有數據,併爲給定形狀添加新的數據。檢查「另存爲」對話框中已存在的文件 - C++,opencascade

請幫我解決這個問題。

回答

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 
    } 

這應該工作,但是如果你用別的東西比追加(有點像寫或者甚至追加,但有更簡單的變體必須是可訪問指定要重寫文件)

HTH,JP

+0

@gishu參數:我修改的代碼片段,包括和使用std命名空間,現在它應該爲你工作的罰款。享受 –

+0

非常感謝。現在它工作正常。 – gishara

相關問題