is_openを利用してファイルのオープン確認を行います。trueが返ってくればファイルはオープンされています。
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
/*
ファイルのオープン確認
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// 標準出力にユニコード出力する
setlocale( LC_ALL, "Japanese" );
// ファイルを開く
std::ifstream ifstr( L"file.txt" );
// オープンできているかを確認
if ( ifstr.is_open() ) {
std::wcout << L"オープンできました。" << std::endl;
}
else {
std::wcout << L"オープンできませんでした。" << std::endl;
}
// オープンできているかを確認(これでもOK)
if ( ifstr ) {
std::wcout << L"オープンできました。" << std::endl;
}
else {
std::wcout << L"オープンできませんでした。" << std::endl;
}
// 正常終了
return( 0 );
}
オープンできました。
オープンできました。