Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将csv读入向量,getline()问题_C++_Csv_Vector_Getline_Long Long - Fatal编程技术网

C++ C++;将csv读入向量,getline()问题

C++ C++;将csv读入向量,getline()问题,c++,csv,vector,getline,long-long,C++,Csv,Vector,Getline,Long Long,我的csv文件数据如下所示: Palmer Shannon,1.66805E+12,7500,3020 Jonas Kelley,1.62912E+12,9068,1496 Jacob Doyle,1.61608E+12,1112,3502 Iola Hayden,1.60603E+12,6826,4194 这是我的头文件: #ifndef client_h #define client_h #include <string> const int MAX_CLIENT = 20;

我的csv文件数据如下所示:

Palmer Shannon,1.66805E+12,7500,3020
Jonas Kelley,1.62912E+12,9068,1496
Jacob Doyle,1.61608E+12,1112,3502
Iola Hayden,1.60603E+12,6826,4194
这是我的头文件:

#ifndef client_h
#define client_h
#include <string>

const int MAX_CLIENT = 20;

struct client {
    std::string name;
    long long  accountNum;
    int pwd;
    double balance;
};
\ifndef客户端
#定义客户端
#包括
const int MAX_CLIENT=20;
结构客户端{
std::字符串名;
长帐户数;
int-pwd;
双平衡;
};
对于第一个数据,字符串名称是指Palmer Shannon,long是指1.66805E+12,int pwd是指7500,double balance是指3020

这是我的main.cpp文件,我尝试创建一个向量来保存csv文件数据

string str;
    std::vector<client> readClientProfile;
    while (getline(data, str))
        {

        client Client;
        istringstream iss(str);
        string token;
        getline(iss, Client.name, ',');

        getline(iss, token, ',');
        Client.accountNum = std::stoi(token);


        getline(iss, token, ',');
        Client.pwd = std::stoi(token);

        getline(iss, token, ',');
        Client.balance = std::stoi(token);


        readClientProfile.push_back(Client);
    }
    for (size_t i = 0; i < readClientProfile.size() - 1; i++)
    {

            std::cout << readClientProfile[i].name << "  "<<endl;
            std::cout << readClientProfile[i].accountNum << "  "<<endl;
            std::cout << readClientProfile[i].pwd << "  "<<endl;
            std::cout << readClientProfile[i].balance << "  "<<endl;

        }
string-str;
std::vector readClientProfile;
while(getline(数据,str))
{
客户;
istringstream iss(str);
字符串标记;
getline(iss,Client.name,,);
getline(iss,token,,);
Client.accountNum=std::stoi(令牌);
getline(iss,token,,);
Client.pwd=std::stoi(令牌);
getline(iss,token,,);
Client.balance=std::stoi(令牌);
readClientProfile.push_back(客户端);
}
对于(size\u t i=0;istd::cout您文件中的帐号存储为
1.66805E+12
。这是一个浮点数,不是整数。当您使用
stoi
将其转换为时,它会解析字符串并在
处停止,因为这不是整数中的有效符号。这意味着
stoi
将返回
1
对于所有账号。您可以首先使用
stod
1.66805E+12
转换为
double
,然后将该
double
存储为整数,如

getline(iss, token, ',');
Client.accountNum = std::stod(token);