AutoFile.h 781 B

12345678910111213141516171819202122232425262728
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. class AutoFILE : public BasePtr<FILE>
  7. {
  8. public:
  9. AutoFILE(FILE * file = nullptr) : BasePtr<FILE>(file) {};
  10. ~AutoFILE()
  11. {
  12. Close();
  13. }
  14. AutoFILE& operator=(FILE * file)
  15. {
  16. Close();
  17. this->ptr = file;
  18. return *this;
  19. }
  20. void Close()
  21. {
  22. if (ptr != nullptr)
  23. {
  24. fclose(ptr);
  25. ptr = nullptr;
  26. }
  27. }
  28. };