C++相談室 part166 (342レス)
上下前次1-新
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん
322(1): デフォルトの名無しさん (ワッチョイ 8101-1tcn) [sage] 2025/06/17(火) 11:27:27.12 ID:2aAnRxo/0(1) AAS
>>321321(3): デフォルトの名無しさん (ワッチョイ b1d6-xkro) [sage] 2025/06/17(火) 11:13:00.43 ID:4NySVCEQ0(1/2) AAS
ファイナルをクローズした時にエラーとなるのですが、これは正しいのでしょうか。
やはり、クローズはデストラクタに任せた方がいいのでしょうか。
#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;
while(std::getline(ifs, buf))
std::cout << buf << std::endl;
ifs.close(); //←これ
if(ifs.fail()){
std::cerr << "File close error: " << path << std::endl;
return -1;
}
return 0;
}
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;
}
上下前次1-新書関写板覧索設栞歴
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル
ぬこの手 ぬこTOP 0.972s*