Advertisement
avr39ripe

cppFileIOChange1to9TEXT

Nov 8th, 2021
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6.     std::fstream file;
  7.  
  8.     file.open("file.txt", std::ios::in | std::ios::out | std::ios::trunc);
  9.  
  10.     if (!file) { std::cout << "IOERROR!\n"; return 1; }
  11.  
  12.     for (int i{ 0 }; i < 10; ++i)
  13.     {
  14.         file << (1000 + rand() % 1000) << '\n';
  15.     }
  16.  
  17.     file.seekg(0, std::ios::beg);
  18.  
  19.     char str[20];
  20.     while (!file.eof())
  21.     {
  22.         file.getline(str, 19);
  23.         std::cout << str << '\n';
  24.     }
  25.  
  26.     file.clear();
  27.     file.seekg(0, std::ios::beg);
  28.  
  29.     char symb{};
  30.     while (!file.eof())
  31.     {
  32.         file.get(symb);
  33.  
  34.         if (symb == '1')
  35.         {
  36.             file.seekp(-1, std::ios::cur);
  37.             file << '9';
  38.             file.seekg(file.tellp(), std::ios::beg);
  39.         }
  40.     }
  41.    
  42.     file.clear();
  43.     file.seekg(0, std::ios::beg);
  44.     while (!file.eof())
  45.     {
  46.         file.getline(str, 19);
  47.         std::cout << str << '\n';
  48.     }
  49.  
  50.     file.close();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement