ฉันต้องโทรด้วยตนเองclose()เมื่อใช้std::ifstreamหรือไม่
ตัวอย่างเช่นในรหัส:
std::string readContentsOfFile(std::string fileName) {
std::ifstream file(fileName.c_str());
if (file.good()) {
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
return buffer.str();
}
throw std::runtime_exception("file not found");
}
ฉันต้องโทรfile.close()ด้วยตนเองหรือไม่ ไม่ควรifstreamใช้RAIIสำหรับการปิดไฟล์?