sammine-lang
Loading...
Searching...
No Matches
FileRAII.h
Go to the documentation of this file.
1//
2// Created by jjasmine on 3/8/24.
3//
4
5#pragma once
6#include <fstream>
7#include <string>
8
12
14class FileRAII {
15public:
16 // Constructor opens the file
17 explicit FileRAII(const std::string &filename)
18 : file(filename), is_opened(true) {
19 is_opened = file.is_open();
20
21 str = std::string((std::istreambuf_iterator<char>(file)),
22 (std::istreambuf_iterator<char>()));
23 }
24
25 // Destructor closes the file
26 ~FileRAII() {
27 if (is_opened) {
28 file.close();
29 }
30 }
31
32 // Function to check if the file is open
33 bool isOpen() const { return is_opened; }
34
35 std::string getInternalStr() { return str; }
36
37private:
38 std::ifstream file;
39 bool is_opened;
40 std::string str;
41};