Skip to content

Instantly share code, notes, and snippets.

@JPGygax68
Last active September 18, 2017 19:35
Show Gist options
  • Save JPGygax68/5928405 to your computer and use it in GitHub Desktop.
Save JPGygax68/5928405 to your computer and use it in GitHub Desktop.
#RIIA wrapper for FILE*. No copy semantics yet.
class FilePtr {
public:
FilePtr() { fp = nullptr; }
FilePtr(FILE *fp_) { fp = fp_; }
FilePtr & operator = (FILE *fp_) { assert(fp == nullptr); fp = fp_; return *this; }
~FilePtr() { if (fp != nullptr) fclose(fp); }
operator FILE * () { return fp; }
private:
FILE *fp;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment