1Kabman1

Такие разные потоки

Jul 19th, 2023
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #include <cassert>
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <string_view>
  6. #include <map>
  7. #include <vector>
  8.  
  9. using namespace std;
  10.  
  11. string SplitLine(const string &line) {
  12.     if (line.empty()) {
  13.         return "";
  14.     }
  15.  
  16.     string result;
  17.  
  18.     for (auto &_char: line) {
  19.         if (_char == '>') {
  20.             continue;
  21.         }
  22.         result += _char;
  23.     }
  24.     return result;
  25.  
  26. }
  27.  
  28.  
  29.  
  30. void CreateFiles(const string &config_file) {
  31.     ifstream ifs(config_file);
  32.  
  33.     map<string, vector<string>> splits;
  34.     string name_file;
  35.     if (!ifs.bad()) {
  36.         string line;
  37.  
  38.         while (getline(ifs, line)) {
  39.  
  40.             if (line.find(".txt"s) != string::npos) {
  41.                 name_file = line;
  42.                 splits[line];
  43.  
  44.             } else {
  45.                 splits[name_file].push_back(SplitLine(line));
  46.  
  47.             }
  48.  
  49.         }
  50.  
  51.         ifs.close();
  52.     }
  53.  
  54.  
  55.     for(auto &[file, text] : splits) {
  56.         if (!ifs.bad()) {
  57.             ofstream ofs(file);
  58.             for(auto &str : text) {
  59.  
  60.                 if (!ofs.bad())
  61.                 {
  62.                     ofs << str<< endl;
  63.                 }
  64.             }
  65.             ofs.close();
  66.         }
  67.     }
  68.  
  69. }
  70.  
  71. string GetLine(istream &in) {
  72.     string s;
  73.     getline(in, s);
  74.     return s;
  75. }
  76.  
  77. int main() {
  78.     ofstream("test_config.txt"s) << "a.txt\n"
  79.                                     ">10\n"
  80.                                     ">abc\n"
  81.                                     "b.txt\n"
  82.                                     ">123"sv;
  83.  
  84.  
  85.     CreateFiles("test_config.txt"s);
  86.     ifstream in_a("a.txt"s);
  87.     assert(GetLine(in_a) == "10"s && GetLine(in_a) == "abc"s);
  88.  
  89.     ifstream in_b("b.txt"s);
  90.     assert(GetLine(in_b) == "123"s);
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment