File IO Functions

This commit is contained in:
R2DLiu 2020-06-30 21:52:44 -04:00
commit 54467c18b1
2 changed files with 23 additions and 0 deletions

View file

@ -34,6 +34,12 @@ IOFile::IOFile(const std::string& filename, const char openmode[]) : m_file(null
Open(filename, openmode);
}
IOFile::IOFile(const std::string& filename, const char openmode[], int shflag)
: m_file(nullptr), m_good(true)
{
OpenShared(filename, openmode, shflag);
}
IOFile::~IOFile()
{
Close();
@ -69,6 +75,19 @@ bool IOFile::Open(const std::string& filename, const char openmode[])
return m_good;
}
bool IOFile::OpenShared(const std::string& filename, const char openmode[], int shflag)
{
Close();
#ifdef _WIN32
m_file = _fsopen(filename.c_str(), openmode, shflag);
#else
m_file = fopen(filename.c_str(), openmode);
#endif
m_good = IsOpen();
return m_good;
}
bool IOFile::Close()
{
if (!IsOpen() || 0 != std::fclose(m_file))

View file

@ -21,6 +21,7 @@ public:
IOFile();
IOFile(std::FILE* file);
IOFile(const std::string& filename, const char openmode[]);
IOFile(const std::string& filename, const char openmode[], int shflag);
~IOFile();
@ -33,6 +34,9 @@ public:
void Swap(IOFile& other) noexcept;
bool Open(const std::string& filename, const char openmode[]);
bool OpenShared(const std::string& filename, const char openmode[], int shflag);
bool Close();
template <typename T>