可以使用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
@gishu參數:我修改的代碼片段,包括和使用std命名空間,現在它應該爲你工作的罰款。享受 –
非常感謝。現在它工作正常。 – gishara