Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <iostream>
- using namespace std;
- class File
- {
- private:
- FILE* f;
- File(const File&) = delete;
- void operator=(const File&) = delete;
- public:
- File(const string& filename)
- {
- f = fopen(filename.c_str(), "w");
- if (f == nullptr)
- {
- throw runtime_error("cannot_open "+filename);
- }
- }
- void Write(const string& line)
- {
- fputs(line.c_str(), f);
- }
- ~File()
- {
- fclose(f);
- }
- };
- int main()
- {
- //// открываем файл и получаем его дескриптор
- //FILE* f = fopen("output.txt", "w");
- //// если не nullptr, то файл успешно открыт
- //if (f != nullptr)
- //{
- // //fprintf(f, "Hello, world!\n");
- // fputs("Hello, world!\n", f);
- // fputs("This file is written with fputs!\n", f);
- // //...
- // fclose(f);//закрываем файл
- //}
- //else
- //{
- // printf("Ошибка при открытии файла!\n");
- //}
- try
- {
- File f("output.txt");
- File f2 = f;
- f.Write("Hello, world!\n");
- f.Write("This is RAII file!\n");
- }
- catch (...)
- {
- printf("Ошибка при открытии файла!\n");
- }
- return 0;
- }
- Advertisement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement