//【初心者歓迎】C/C++室 Ver.80【環境依存OK】
//http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1348161305/458
//
//458 名前:デフォルトの名無しさん[sage] 投稿日:2012/10/26(金) 18:46:38.87
//ofstreamまたはifstreamでファイルが開けなかった時にcloseする必要ってありますか?
//
//// 例
//ofstream ofs("test.txt");
//if (ofs.fail()) {
// ofs.close(); // ←これ
//}
//Example for VS2010C++
#include <fstream>
#include <ios>
#include <iostream>
#include <Shlwapi.h>
#pragma comment(lib, "Shlwapi.lib")
void FileExist(const char fname[])
{
bool fileexists;
fileexists = (::PathFileExistsA(fname) == TRUE ? true : false);
std::cout << fname << " " << (fileexists ? "" : "does not ") << "exist" << (fileexists ? "s" : "") << "." << std::endl;
return;
}
void ShowStates(std::ifstream *ifs)
{
std::cout << "\tifs.is_open() is " << ifs->is_open() << "." << std::endl;
std::cout << "\tstate of bits: " << ((ifs->rdstate() & std::ios_base::eofbit) == std::ios_base::eofbit ? "eof" : " ");
std::cout << " " << ((ifs->rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "fail" : " ");
std::cout << " " << ((ifs->rdstate() & std::ios_base::badbit) == std::ios_base::badbit ? "bad" : " ");
std::cout << " " << (ifs->rdstate() == std::ios_base::goodbit ? "good" : "") << std::endl;
std::cout << "\tifs.fail() is " << ifs->fail() << "." << std::endl;
return;
}
int main(void)
{
const char ifname1[] = "main.cpp";
const char ifname2[] = "xxx";
const char ifname3[] = "yyy";
FileExist(ifname1);
FileExist(ifname2);
FileExist(ifname3);
std::cout << std::endl;
std::cout << std::boolalpha;
std::cout << "Example 1:" << std::endl;
{
std::ifstream ifs(ifname1);
std::cout << "Create std::ifstream ifs with \"" << ifname1 << "\"." << std::endl;
ShowStates(&ifs);
ifs.open(ifname2);
std::cout << "ifs.open(" << ifname2 << ") is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
ifs.clear();
std::cout << "Clear state of bits." << std::endl;
ShowStates(&ifs);
ifs.close();
std::cout << "ifs.close() is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
std::cout << std::endl;
std::cout << "Example 2:" << std::endl;
{
std::ifstream ifs(ifname1);
std::cout << "Create std::ifstream ifs with \"" << ifname1 << "\"." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->open(" << ifname2 << ") is " << (ifs.rdbuf()->open(ifname2, std::ios_base::in) == NULL ? "False" : "Success") << "." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->close() is " << (ifs.rdbuf()->close() == NULL ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
std::cout << std::endl;
std::cout << "Example 3:" << std::endl;
{
std::ifstream ifs(ifname1);
std::cout << "Create std::ifstream ifs with \"" << ifname1 << "\"." << std::endl;
ShowStates(&ifs);
ifs.close();
std::cout << "ifs.close() is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
ifs.open(ifname2);
std::cout << "ifs.open(" << ifname2 << ") is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
ifs.close();
std::cout << "ifs.close() is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
std::cout << std::endl;
std::cout << "Example 4:" << std::endl;
{
std::ifstream ifs(ifname1);
std::cout << "Create std::ifstream ifs with \"" << ifname1 << "\"." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->close() is " << (ifs.rdbuf()->close() == NULL ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->open(" << ifname2 << ") is " << (ifs.rdbuf()->open(ifname2, std::ios_base::in) == NULL ? "False" : "Success") << "." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->close() is " << (ifs.rdbuf()->close() == NULL ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
std::cout << std::endl;
std::cout << "Example 5:" << std::endl;
{
std::ifstream ifs(ifname3);
std::cout << "Create std::ifstream ifs with \"" << ifname3 << "\"." << std::endl;
ShowStates(&ifs);
ifs.clear();
std::cout << "Clear state of bits." << std::endl;
ShowStates(&ifs);
ifs.close();
std::cout << "ifs.close() is " << ((ifs.rdstate() & std::ios_base::failbit) == std::ios_base::failbit ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
std::cout << std::endl;
std::cout << "Example 6:" << std::endl;
{
std::ifstream ifs(ifname3);
std::cout << "Create std::ifstream ifs with \"" << ifname3 << "\"." << std::endl;
ShowStates(&ifs);
std::cout << "ifs.rdbuf()->close() is " << (ifs.rdbuf()->close() == NULL ? "Fail" : "Success") << "." << std::endl;
ShowStates(&ifs);
}
return 0;
}
//OUTPUT
//==========
//main.cpp exists.
//xxx exists.
//yyy does not exist.
//
//Example 1:
//Create std::ifstream ifs with "main.cpp".
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.open(xxx) is Fail.
// ifs.is_open() is true.
// state of bits: fail
// ifs.fail() is true.
//Clear state of bits.
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//
//Example 2:
//Create std::ifstream ifs with "main.cpp".
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.rdbuf()->open(xxx) is False.
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.rdbuf()->close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//
//Example 3:
//Create std::ifstream ifs with "main.cpp".
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//ifs.open(xxx) is Success.
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//
//Example 4:
//Create std::ifstream ifs with "main.cpp".
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.rdbuf()->close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//ifs.rdbuf()->open(xxx) is Success.
// ifs.is_open() is true.
// state of bits: good
// ifs.fail() is false.
//ifs.rdbuf()->close() is Success.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//
//Example 5:
//Create std::ifstream ifs with "yyy".
// ifs.is_open() is false.
// state of bits: fail
// ifs.fail() is true.
//Clear state of bits.
// ifs.is_open() is false.
// state of bits: good
// ifs.fail() is false.
//ifs.close() is Fail.
// ifs.is_open() is false.
// state of bits: fail
// ifs.fail() is true.
//
//Example 6:
//Create std::ifstream ifs with "yyy".
// ifs.is_open() is false.
// state of bits: fail
// ifs.fail() is true.
//ifs.rdbuf()->close() is Fail.
// ifs.is_open() is false.
// state of bits: fail
// ifs.fail() is true.