shushus

Спринт 12. Тонкости открытия файлов.

Apr 6th, 2023
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 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. size_t GetFileSize(string file) {
  10.     fstream fil(file, ios::in | ios::out);
  11.     fil.seekg(0, ios::end);
  12.  
  13.     return static_cast<size_t>(fil.tellg());
  14. }
  15.  
  16. int main() {
  17.     ofstream("test.txt") << "123456789"sv;
  18.     assert(GetFileSize("test.txt"s) == 9);
  19.  
  20.     ofstream test2("test2.txt"s);
  21.     test2.seekp(1000);
  22.     test2 << "abc"sv;
  23.     test2.flush();
  24.  
  25.     assert(GetFileSize("test2.txt"s) == 1003);
  26.     assert(GetFileSize("a file not exists"s) == string::npos);
  27. }
  28.  
Advertisement
Add Comment
Please, Sign In to add comment