Advertisement
avr39ripe

cppFileIOAdvTellGSeekG

Jun 16th, 2021
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. /*
  5.  
  6. std::ios::app -- append to file
  7. std::ios::in - open file for input, reading
  8. std::ios::out - open file for output, writeing
  9. std::ios:binary - opens file in binary mode
  10. std::ios::ate - opens file and moves file pointer to END, pr AT ehe End
  11. std::ios::trunc - removes file if exist
  12.  
  13. */
  14.  
  15. // iofile.seekg(iofile.tellg(), std::ios::beg)
  16. int main()
  17. {
  18.     std::fstream iofile;
  19.     iofile.open("test.txt", std::ios::in | std::ios::out); //| std::ios::ate
  20.  
  21.     //iofile.seekg(0, std::ios::end);
  22.  
  23.     //std::cout << "Current reading position: " << iofile.tellg() << '\n';
  24.  
  25.     //iofile.seekg(12, std::ios::beg);
  26.  
  27.     //std::cout << "First move Current reading position: " << iofile.tellg() << '\n';
  28.  
  29.     //iofile.seekg(-7, std::ios::cur);
  30.  
  31.     //std::cout << "Second move Current reading position: " << iofile.tellg() << '\n';
  32.     char buf[100];
  33.     char symbol{};
  34.  
  35.     while (iofile.get(symbol))
  36.     {
  37.         if (symbol == '#')
  38.         {
  39.             iofile.seekp(((int)iofile.tellg() - 1), std::ios::beg);
  40.             iofile << 't';
  41.             iofile.seekg(iofile.tellg(), std::ios::beg);
  42.         }
  43.     }
  44.  
  45.     //while (!iofile.eof())
  46.     //{
  47.     //  iofile.getline(buf, 100);
  48.     //  std::cout << buf << '\n';
  49.     //  std::cout << "Current reading position: " << iofile.tellg() << '\n';
  50.     //}
  51.  
  52.     std::cout << "Current reading position: " << iofile.tellg() << '\n';
  53.  
  54.     iofile.close();
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement