Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cassert>
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <string_view>
- #include <map>
- #include <vector>
- using namespace std;
- string SplitLine(const string &line) {
- if (line.empty()) {
- return "";
- }
- string result;
- for (auto &_char: line) {
- if (_char == '>') {
- continue;
- }
- result += _char;
- }
- return result;
- }
- void CreateFiles(const string &config_file) {
- ifstream ifs(config_file);
- map<string, vector<string>> splits;
- string name_file;
- if (!ifs.bad()) {
- string line;
- while (getline(ifs, line)) {
- if (line.find(".txt"s) != string::npos) {
- name_file = line;
- splits[line];
- } else {
- splits[name_file].push_back(SplitLine(line));
- }
- }
- ifs.close();
- }
- for(auto &[file, text] : splits) {
- if (!ifs.bad()) {
- ofstream ofs(file);
- for(auto &str : text) {
- if (!ofs.bad())
- {
- ofs << str<< endl;
- }
- }
- ofs.close();
- }
- }
- }
- string GetLine(istream &in) {
- string s;
- getline(in, s);
- return s;
- }
- int main() {
- ofstream("test_config.txt"s) << "a.txt\n"
- ">10\n"
- ">abc\n"
- "b.txt\n"
- ">123"sv;
- CreateFiles("test_config.txt"s);
- ifstream in_a("a.txt"s);
- assert(GetLine(in_a) == "10"s && GetLine(in_a) == "abc"s);
- ifstream in_b("b.txt"s);
- assert(GetLine(in_b) == "123"s);
- }
Advertisement
Add Comment
Please, Sign In to add comment