c++ getline读取避免空行

180it 2020-10-13 AM 25℃ 0条
#include<fstream>
#include<iostream>
#include<string>
#include <vector>
using namespace std;
vector< string> split(string str, string pattern)
{
    vector<string> ret;
    if (pattern.empty()) return ret;
    size_t start = 0, index = str.find_first_of(pattern, 0);
    while (index != str.npos)
    {
        if (start != index)
            ret.push_back(str.substr(start, index - start));
        start = index + 1;
        index = str.find_first_of(pattern, start);
    }
    if (!str.substr(start).empty())
        ret.push_back(str.substr(start));
    return ret;
}
int main()
{
    string pattern = " ";
    vector <string> result;
    string name;
    ifstream in("C:/Users/Administrator/Desktop/save_paper.txt");
    while (!in.eof())
    {
        getline(in, name);
        if (name.empty())
        {
            continue;
        }
        result = split(name, pattern);
        cout << result[0] << endl;
    }
}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

c++ getline读取避免空行