Advertisement
Guest User

Untitled

a guest
Jan 15th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.     // Открываем в режиме чтения, записи и с конца файла
  10.     fstream inOut ("copyOut.txt", fstream::ate | fstream::in | fstream::out);
  11.  
  12.     if (!inOut) {
  13.         cerr << "Unable to open file!" << endl;
  14.         return EXIT_FAILURE;
  15.     }
  16.  
  17.     auto end_mark = inOut.tellg();
  18.     inOut.seekg(0, fstream::beg); // в начало
  19.     size_t cnt = 0; // счётчик символов (байт)
  20.     string line;
  21.     while (inOut && inOut.tellg() != end_mark && getline(inOut, line)) {
  22.         cnt += line.size() + 1; // учитываем символ новой строки
  23.         auto mark = inOut.tellg();
  24.         inOut.seekp(0, fstream::end);
  25.         inOut << cnt;
  26.  
  27.         if (mark != end_mark) inOut << " ";
  28.         inOut.seekg(mark);
  29.     }
  30.     inOut.seekp(0, fstream::end);
  31.     inOut << "\n";
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement