【发布时间】:2019-04-18 17:08:18
【问题描述】:
我是编程和 C++ 的初学者。
我有一个 typedef struct Player,其中有 5 个玩家作为数组,如下所示:
类型定义结构{ int id_player; 字符串昵称; 字符首字母[4]; 积分; }播放器; 玩家玩家[5];
我有一个 csv 文件,其中包含以下条目:
1002 毒刺 FMH 12980
其中:int id、string、char 首字母[4]、int score 由制表符分隔
我必须从 csv 中的每个条目中获取数据并填写玩家值。
有类似的问题,但我不知道如何处理它们,我必须使用 C++ 而不是 C。我是初学者,可能是我写了无意义的代码,但我希望我的问题有用对其他人,请不要投票给我。
我正在尝试使用 ostreamstring 输入序列来执行此操作,但我不知道这是否是一个好方法。我不知道该怎么做。 这就是我所拥有的:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
using std::ios;
#include <sstream>
using std::ostringstream;
typedef struct{
int id_player;
string nickname;
char initials[4];
int score;
}Player;
Player players[5];
int main(){
int id_player_aux;
string nickname_aux;
char initials_aux[4];
int score_aux;
string line;
ostringstream entry;
entry << id_player_aux << nickname_aux << initials_aux << score_aux;
ifstream data("test.csv", ios::in);
while(getline(data, line)){
entry << line;
}
return 0;
}
谢谢。
【问题讨论】:
-
是否要从文件中获取数据并将其分配给播放器对象?
-
是的,这就是我想要的。 csv 中的每个条目分配给每个玩家的每个值,因此,在示例中,player[1] 从 csv 获取: player[1].id_player= 1002, player[1].nickname = Stingger, player[1] .initials = FMH player[1].score = 12980。下一个条目,下一个玩家。
标签: c++