Advertisement
avr39-ripe

fileIO

May 4th, 2020
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. int main()
  5. {
  6.     std::ofstream outf;
  7.  
  8.     //std::ios::app - append to file
  9.     //std::ios::ate - at the end - go to the end of file
  10.     //std::ios::binary - open file in binary mode
  11.     //std::ios::in - open file to read - input mode default for ifstream
  12.     //std::ios::out - open file for writing - output mode default for ofstream
  13.     //std::ios::trunc - delete file if exist
  14.  
  15.     outf.open("file.txt");
  16.     if (!outf) { std::cout << "IO ERROR! cannot open file!\n"; return 1; };
  17.  
  18.     //outf << "Hello, from C++ file io app!\n";
  19.     //outf << "One more line of text saved in file\n";
  20.     //outf << "Last line! Thats all for now!\n";
  21.     //int num{ 100500 };
  22.  
  23.     //for (int i{0}; i < 4; ++i)
  24.     //{
  25.     //  std::cout << (int)*((uint8_t*)(&num + i)) << '\n';
  26.     //};
  27.  
  28.     float val{};
  29.     for (int i{0}; i < 10; ++i)
  30.     {
  31.         val = ((float)i / (rand() % 100 + 1));
  32.  
  33.         std::cout << "Line number: ";
  34.         std::cout << i << '\n';
  35.         //<< val << '\n';
  36.         outf << "Line number: ";
  37.         outf << i << '\n';
  38.         //' ' << val << '\n';
  39.     }
  40.  
  41.     outf.close();
  42.  
  43.     std::fstream iof;
  44.     iof.open("file.txt", std::ios::in | std::ios::out | std::ios::ate);
  45.     if (!iof) { std::cout << "IO ERROR! cannot open file!\n"; return 1; };
  46.  
  47.     std::cout << "\nTry to read file content: \n";
  48.     char buf[200];
  49.     int numI{};
  50.     float numF{};
  51.  
  52.     // seekg(pos, rel) - move read (get) position
  53.     // seekp(pos. rel) - move write (put) position
  54.     // tellg() - say read (get) position
  55.     // tellp() - say write (put) position
  56.     // REl
  57.     // std::ios:beg
  58.     // std::ios:end
  59.     // std::ios:cur
  60.  
  61.     std::cout << "\nRead pos: " << iof.tellg() << "\nWrite pos: " << iof.tellp() << '\n';
  62.     iof.seekp(-(iof.tellp()), std::ios::end);
  63.     iof << "Some line of text! :)";
  64.     iof.seekg(0, std::ios::beg);
  65.     std::cout << "\nRead pos: " << iof.tellg() << "\nWrite pos: " << iof.tellp() << '\n';
  66.  
  67.     while (!iof.eof())
  68.     {
  69.         iof.getline(buf,200);
  70.         std::cout << buf << '\n';
  71.         //inf >> numI;
  72.         //if (inf.fail()) break;
  73.         //inf >> numF;
  74.         //if (inf.fail()) break;
  75.         //std::cout << buf << ' ';
  76.         //std::cout << numI << ' ' << numF << '\n';
  77.         //inf.ignore(100, '\n');
  78.     }
  79.  
  80.     iof.close();
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement