この単純な関数を使用できます。
#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