- 322 名前:デフォルトの名無しさん mailto:sage [2025/06/17(火) 11:27:27.12 ID:2aAnRxo/0.net]
- >>321
ifs.close()前にifs.fail()は真を返す #include <iostream> #include <fstream> int main() { constexpr auto path = "tmp.txt";//なんでもいいです std::ifstream ifs(path); if(ifs.fail()){ std::cerr << "File open error: " << path << std::endl; return -1; } std::string buf; std::cout << "0: " << ifs.fail() << '\n'; while(std::getline(ifs, buf)) std::cout << buf << std::endl; std::cout << "1: " << ifs.fail() << '\n'; ifs.close(); //←これ std::cout << "2: " << ifs.fail() << '\n'; if(ifs.fail()){ std::cerr << "File close error: " << path << std::endl; return -1; } return 0; }
|

|