militaryCoder

Correct reading using fstream

Sep 28th, 2021
563
4 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <fstream>
  2. #include <string>
  3. #include <map>
  4. #include <iostream>
  5.  
  6. int main() {
  7.     std::ifstream in("inp.txt");
  8.     std::ofstream out("out.txt");
  9.     std::map<std::string, int> v;
  10.  
  11.     if (in.good()) {
  12.         std::string buf;
  13.         unsigned i;
  14.         while (in >> buf >> i) {
  15.             v.push_back({buf, i});
  16.         }
  17.     }
  18.     in.close();
  19.  
  20.     for (const auto &p : v) {
  21.         out << p.first << " " << p.second << '\n';
  22.     }
  23.  
  24.     out.close();
  25.     return 0;
  26. }
RAW Paste Data