Advertisement
avr39ripe

cppFileIOBase

Jun 15th, 2021
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. /*
  5. * ifstream
  6. * ofstream
  7. * fstream
  8. */
  9.  
  10.  
  11. int main()
  12. {
  13.     std::ofstream outf;
  14.  
  15.     outf.open("file.txt");
  16.  
  17.     if (!outf) { std::cout << "IO ERROR!\n"; return 1; }
  18.    
  19.     //outf << "Hello, from C++ file io programm!\n";
  20.     //outf << "Second string" << '\n';
  21.     //outf << "Some numbers " << 123 << '\n';
  22.    
  23.     for (int i{ 100000 }; i < 100005; ++i)
  24.     {
  25.         //outf << "Line # " << i << '\n';
  26.         outf << i << '\n';
  27.     }
  28.  
  29.     //outf << "Some bool value " << (true ? "true" : "false") << '\n';
  30.     //outf << "Some double literal " << 36.6 << '\n';
  31.     outf.close();
  32.  
  33.     char str[200];
  34.     int readVal{};
  35.     std::ifstream inf;
  36.  
  37.     inf.open("file.txt");
  38.     if (!inf) { std::cout << "IO ERROR!\n"; return 1; }
  39.  
  40.     //while (!inf.eof())
  41.     //{
  42.     //  inf.getline(str, 200);
  43.     //  std::cout << str << '\n';
  44.     //}
  45.  
  46.     while (!inf.eof())
  47.     {
  48.         inf >> readVal;
  49.         if (inf.fail()) { break; }
  50.         std::cout << "Read from file: " << readVal << '\n';
  51.         //inf.ignore(100, '\n');
  52.     }
  53.  
  54.     inf.close();
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement