#ifndef FILE_DESCRIPTOR_H_ #define FILE_DESCRIPTOR_H_ #include #include #include class FileDescriptor { public: explicit FileDescriptor(const char* file) { // Constructor fd_ = open(file, O_RDONLY); // On error, we get fd_ = -1 // error handling is in the Destructor } ~FileDescriptor() { // Destructor // No need to close an invalid descriptor if (fd_ != -1) { int res = close(fd_); if (res == -1) { std::cerr << "Error Closing File" << std::endl; } } } int get_fd() const { return fd_; } // inline member function private: int fd_; // data member }; // class FileDescriptor #endif // FILE_DESCRIPTOR_H_