C++按行读取并处理字符串

//
// Created by 13989 on 2022/10/26.
//

#include <iostream>
#include <fstream>
#include <istream>
#include <sstream>
#include <string>
#include "string_util.h"

using namespace std;

void test()
{
    string file1 = "D:\\myProject\\k0.log";
    ifstream ifile;
    ifile.open(file1, ios::in);
    if (!ifile.is_open()) {
        return;
    }
    string str;
    while (getline(ifile, str)) {
        cout << str << endl;
        vector<string> res;
        StringSplit(str, '(', res);
        for (int i = 1; i < res.size(); i++) {
            res[i].replace(res[i].find(')'), 1, "");
        }
        for (auto s : res) {
            cout << s << endl;
        }
    }
    ifile.close();
}
void StringSplit(std::string str, char cha, std::vector<std::string>& res)
{
    istringstream iss(str);
    string token;
    while (getline(iss, token, cha)) {
//        cout << token << endl;
        res.push_back(token);
    }
}

void StringReplace(std::string& str, char x, const string y)
{
    str.replace(str.find(x), 1, y);
}


版权声明:本文为qq_26552071原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。