I'm reviewing a project that does file input/output operations in C++. There are uses for the overloaded ! operator defined in std::ios that I have not encountered before. I know that the ! operator is used to check if a file has been opened. However, I did not understand why the author used the fstream object by using the ! operator after using the istream::seekg, istream::read, ostream::seekp, ostream::write methods in the project I was examining.
Below is a part of the add() function in the source code I've reviewed:
#include <fstream>
#include <iostream>
bool add(std::fstream &file, std::istream &input)
{
file.seekg((id - 1) * sizeof(Person));
/* What is the purpose of using the "operator!" below? */
if(!file){ return false; }
file.read(reinterpret_cast<char *>(&temp), sizeof(Person));
/* What is the purpose of using the "operator!" below? */
if(!file){ return false; }
file.seekp((id - 1) * sizeof(Person));
/* What is the purpose of using the "operator!" below? */
if(!file){ return false; }
file.write(reinterpret_cast<const char*>(&person), sizeof(Person));
/* What is the purpose of using the "operator!" below? */
if(!file){ return false; }
}
Do the above uses of the operator! operator make any sense?