C++读取带逗号分隔符的数据文件

C++读取带逗号分隔符的数据文件,第1张

C++读取带逗号分隔符的数据文件

txt文件内容:

#include 
#include 
#include 
#include 
#include 
using namespace std;


int main() {
    vector  > data;
    ifstream infile("D:\桌面文件\test.txt");

    while (infile)
    {
        string s;
        if (!getline(infile, s)) break;

        istringstream ss(s);
        vector  record;

        while (ss)
        {
            string s;
            if (!getline(ss, s, ',')) break;
            record.push_back(s);
            
        }
        for (auto x : record) cout << atof(x.c_str()) << " ";//string转double
        cout << endl;
        data.push_back(record);
    }
    
    if (!infile.eof())
    {
        cerr << "Fooey!n";
    }
}

输出

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5115424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存