Advertisement
kutuzzzov

Урок 4 тонкости открытия файлов

May 24th, 2023
1,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <cassert>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <string_view>
  6.  
  7. using namespace std;
  8.  
  9. // реализуйте эту функцию:
  10. size_t GetFileSize(string file) {
  11.     ifstream in(file, ios::in);
  12.     if (in) {
  13.         in.seekg(0, ios::end);
  14.         return static_cast<size_t>(in.tellg());
  15.     }
  16.     return string::npos;
  17. }
  18.  
  19. int main() {
  20.     ofstream("test.txt") << "123456789"sv;
  21.     assert(GetFileSize("test.txt"s) == 9);
  22.  
  23.     ofstream test2("test2.txt"s);
  24.     test2.seekp(1000);
  25.     test2 << "abc"sv;
  26.     test2.flush();
  27.  
  28.     assert(GetFileSize("test2.txt"s) == 1003);
  29.     assert(GetFileSize("a file not exists"s) == string::npos);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement