CSV文件导入到string和double

时间:2017-04-10 22:59:42

标签: c++

我正在尝试编写自己的投资软件。我坚持从CSV文件导入当前数据的最佳方法。我可以使用数组或向量,但我当前的编码使用数组。我遇到的问题是a)清理代码,b)将数字导入为数字副字符串。这是我的CSV文件的片段,因此您可以看到我正在使用的内容:

QRETQ,FDIC保险账户 - IRA," 6.000"," $ 1.00"," 0.00(0.00%)"," $ 0.00 "" N / A"" $ 6.0的#34; IJJ,ISHARES S& P," 4.013"," $ 147.70"," 0.56(0.38%)"," $ 2.25" " - $ 20.18"" $ 592.67"

我现在的代码。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;

int main()
{
string symbol[100];
string descrip[100];
string quantity[100];
string price[100];
string change[100];
string dayGainLoss[100];
string estGainLoss[100];
string marketBal[100];
string dividend[100];
string ph;


ifstream file ( "jroth.csv" );

    while ( file.good() )
    {
        for (int i=0; i <= 7; i++)
        {
            getline ( file, symbol[i], ',' );
            getline ( file, descrip[i], ',' );
            getline ( file,  ph, '"' );
            getline ( file, quantity[i], '"' );
            getline ( file,  ph, ',' );
            getline ( file,  ph, '"' );
            getline ( file,  ph, '$' );
            getline ( file, price[i], '"' );
            double buffer;
            buffer = stoi(price[i].data());
            getline ( file,  ph, ',' );
            getline ( file,  ph, '"' );
            getline ( file, change[i], '"' );
            getline ( file,  ph, ',' );
            getline ( file,  ph, '"' );
            getline ( file, dayGainLoss[i], '"' );
            getline ( file,  ph, ',' );
            getline ( file,  ph, '"' );
            getline ( file, estGainLoss[i], '"' );
            getline ( file,  ph, ',' );
            getline ( file,  ph, '"' );
            getline ( file,  ph, '$' );
            getline ( file, marketBal[i], '"' );
        }
    }

}

0 个答案:

没有答案