【问题标题】:Get different types of data from a csv file and put it in a typedef struct in C++从 csv 文件中获取不同类型的数据并将其放入 C++ 中的 typedef 结构中
【发布时间】: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++


【解决方案1】:

这里有一个不同的解决方案:它逐行检索玩家信息,然后将它们存储到GetPlayerInformation()中的struct标签中。

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

const char TAB = '\t';

struct struct_player {
    int id_player;
    string nickname;
    string initials;
    int score;
};

void GetPlayerInformation(stringstream & stringstream_player, struct_player & player) {
    string string_id_player, string_score;

    getline(stringstream_player, string_id_player, TAB);
    getline(stringstream_player, player.initials, TAB);
    getline(stringstream_player, player.nickname, TAB);
    getline(stringstream_player, string_score, TAB);

    stringstream(string_id_player) >> player.id_player;
    stringstream(string_score) >> player.score;
}

int main() {
    struct_player player;
    string line_player;
    ifstream file_players("players.csv");
    struct_player players[5];

    for(int i = 0; i < 5; i++)
        if(getline(file_players,line_player)) {
            stringstream stringstream_player(line_player);
            GetPlayerInformation(stringstream_player, players[i]);
            cout << players[i].id_player << ", " << players[i].initials << ", " <<
                    players[i].nickname << ", " << players[i].score << endl;
        }
}

【讨论】:

    【解决方案2】:

    您可能会为此找到一个副本,但如果我了解您想要什么,您只需要一种将文件中的输入存储到播放器数组的方法。关于 std::fstream(代表文件流)的研究。这是一个示例程序:

    #include <iostream>
    #include <fstream>
    
    typedef struct{
    
        int id_player;
        std::string nickname;
        char initials[4];
        int score;
    
    }Player;
    
    Player players[5];
    
    int main(){
    
        std::ifstream file("data.csv");
    
        if(!file.is_open()){
            std::cout << "Could not open the file";
            return 1;
        }
    
        for(int i = 0; i < 5; ++i) {
            file >> players[i].id_player >> players[i].nickname >> players[i].initials >> players[i].score;
        }
    
        for(int i = 0; i < 5; ++i) {
            std::cout << players[i].id_player << " "
                  << players[i].nickname << " "
                  << players[i].initials << " "
                  << players[i].score << "\n";
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我可以建议为 Player 重载 operator&gt;&gt;(),如下所示:

      istream& operator >>(istream& stream, Player& p) {
          stream >> id_player_aux >> nickname_aux >> initials_aux >> score_aux;
      
          return stream;
      }
      

      这样,在你的主要,你可以做这样的事情:

      for(int i=0;i<MAX_PLAYERS/* in this case, 5 */;i++) {
          data >> players[i];
      }
      

      或类似的东西。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-18
        • 2014-07-10
        • 2016-07-31
        • 2013-09-11
        • 1970-01-01
        • 2013-08-06
        相关资源
        最近更新 更多