我正在尝试将 csv 文件中的数据读取到一个结构向量中,每个结构向量都包含来自足球比赛的各种值(进攻球队、四分之一、向下、要走的码数、播放 desc 等)。我正在尝试使用 stringstream 将字符串转换为整数,但我正在返回垃圾数据。
Data d; //structs
vector<Data> TeamData; //vector of structs
string s;
istringstream iss( s );
ifstream myfile;
myfile.open("CSV file here"); //open CSV file
while ( !myfile.eof() )
{
getline (myfile, d.gameid, ','); //read to comma, store in d.gameid
getline (myfile, s, ','); //read to comma
iss >> d.qtr; //use stringstream to store into d.qtr
repeat for remaining values . . .
如果您想知道,cvs 文件看起来与此类似:
20070906_NO@IND,1,47,25,IND,NO,1,10,31,(2:25) J.Addai up the middle to NO 27 for 4 yards (S.Fujita).,0,0,2007
20070906_NO@IND,1,46,42,IND,NO,2,6,27,(1:42) P.Manning pass deep left to M.Harrison for 27 yards TOUCHDOWN.,0,0,2007
当我尝试输出所有相关内容时,我的程序会返回这样的数据:
Game ID: 20070906_NO@IND
Offensive team: IND
Quarter: 7077994
Down: 4696320
To go: 4670480
Play: (2:25) J.Addai up the middle to NO 27 for 4 yards (S.Fujita).
Game ID: 20070906_NO@IND
Offensive team: IND
Quarter: 7077994
Down: 4696320
To go: 4670480
Play: (1:42) P.Manning pass deep left to M.Harrison for 27 yards TOUCHDOWN.