Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. abcd
  2. efg
  3. hi
  4. j
  5.  
  6. abcd
  7. efg
  8. hi
  9. j
  10. 5 9 12 14
  11.  
  12. abcd
  13. efg
  14. hi
  15. j
  16. 5 6 7 8
  17.  
  18. #include <iostream>
  19. #include <fstream>
  20. using namespace std;
  21.  
  22. int main(int argc, char **argv)
  23. {
  24. // Prepare file
  25. fstream f(argv[1], fstream::ate | fstream::in | fstream::out);
  26. if(!f)
  27. {
  28. cerr << "Unable to open file!" << endl;
  29. return EXIT_FAILURE;
  30. }
  31.  
  32. // Prepare needed data for production
  33. auto end_mark = f.tellg();
  34. size_t count{0};
  35. char c[64];
  36. // Go back to the beginning
  37. f.seekg(0, fstream::beg);
  38.  
  39. while(f && f.tellg() != end_mark && f.getline(c, 64))
  40. {
  41. count += f.gcount();
  42.  
  43. auto mark = f.tellg();
  44. // Go to the end
  45. f.seekp(0, fstream::end);
  46.  
  47. f << count;
  48. if(mark != end_mark) f << " ";
  49.  
  50. // Go back to the last line
  51. f.seekp(mark);
  52. }
  53.  
  54. f.seekp(0, fstream::end);
  55. f << 'n';
  56.  
  57. system("PAUSE");
  58. return 1;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement