从文件中读取到结构的数组 [英] Reading from a file into an array of structs

查看:72
本文介绍了从文件中读取到结构的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说,这是对我的CS161类的分配,因此,尽管直接回答将是很好的,一个很好的解释,将有助于我比什么都重要。这个星期,我们已经覆盖结构,但我有我的一些code麻烦。现在我的目标,该计划是由一个名为QBInfo.txt文件的读取三个结构数组。

 结构四分卫{
    字符串名称;
    INT完井[kNumGames]
    INT试图[kNumGames]
    INT码[kNumGames]
    INT达阵[kNumGames]
    INT拦截[kNumGames]
};四分卫球员[kNumPlayers] = {PLAYER1,player2,player3};

另外,如果它可以帮助我应该说,kNumPlayers是一个常数设置为3,和kNumGames是一个常数设置为10。

这是code我使用从我的文件中读取。它似乎并不正确,虽然工作,我一直在亏损,为什么,所以我想我可能会要求社区提供一些帮助。

  ifstream的myIF;
    myIF.open(QBInfo.txt);
    串垃圾;
    串温度;
    函数getline(myIF,垃圾);
    的for(int i = 0; I< kNumPlayers;我++){
        函数getline(myIF,温度);
        玩家[我]。名称=温度;
        对于(INT J = 0; I< kNumGames; J ++){
            myIF>>玩家[I] .completions [J]。
            myIF>>玩家[I] .attempts [J]。
            myIF>>玩家[I] .yards [J]。
            myIF>>玩家[I] .touchdowns [J]。
            myIF>>玩家[I] .interceptions [J]。
        }
    }

下面是我的老师提供了测试用例文件。第一个数字是,我会在以后尝试由于时间限制转让的挑战部分。这是函数getline(myIF,垃圾)的原因我的循环之前code的部分开始。

  3
佩顿·曼宁
27 42 462 7 0
30 43 307 0 2
32 37 374 3 0
28 34 327 0 4
33 42 414 4 1
28 42 295 2 1
29 49 386 3 1
30 44 354 4 3
25 36 330 0 4
24 40 323 1 0
汤姆·布雷迪
29 52 288 2 1
19 39 185 1 0
25 36 225 2 1
20 31 316 0 2
18 38 197 0 1
25 43 269 1 1
22 46 228 0 1
13 22 116 11
23 33 432 0 4
29 40 296 1 1
易建联
26 35 357 2 1
26 46 322 1 2
29 46 342 3 1
30 39 413 0 4
29 35 288 0 2
17 36 236 2 1
26 34 332 0 5
30 51 382 2 2
34 41 392 0 4
30 43 305 1 1


解决方案

使用 istringstream 来填充从文件中读取的数据结构成员。
命名可能不会在短节目中特别重要,如这一点 - 但它是想传达的意思变量名的好习惯。
数据文件似乎有在第一线的玩家数量。函数getline被用来读取过去的这条线。通常,将被用于初始化数的播放器变量

 的std :: ifstream的INFILE;
infile.open(qb.dat);
标准::字符串nPlayers;
标准::字符串playerName;函数getline(INFILE,nPlayers);
函数getline(INFILE,playerName);对于(INT玩家= 0;播放器和LT; kNumPlayers ++播放器)
{
  对于(INT游戏= 0;游戏和LT; kNumGames ++游戏)
  {
    标准::字符串数据行;
    函数getline(INFILE,数据行);
    的std :: istringstream是(数据行);
    是>>玩家[播放] .completions [游戏]>>
          玩家[播放] .attempts [游戏]>>
          玩家[播放] .yards [游戏]>>
          玩家[播放] .touchdowns [游戏]>>
          玩家[播放] .interceptions [游戏]
  }
}

First off, I would like to say that this is an assignment for my CS161 class, so while a direct answer would be nice, a nice explanation will help me more than anything. We have covered structs this week, but I am having trouble with some of my code. My goal right now for the program is to read from a file called "QBInfo.txt" into an array of three structures.

struct QuarterBack{
    string name;
    int completions[kNumGames];
    int attempts[kNumGames];
    int yards[kNumGames];
    int touchdowns[kNumGames];
    int interceptions[kNumGames];
};

QuarterBack players[kNumPlayers] = {player1, player2, player3};

Also, if it helps I should say that kNumPlayers is a constant set to 3, and kNumGames is a constant set to 10.

This is the code I am using to read from my file. It doesn't seem to work right though, and I have been at a loss as to why, so I figured I might ask the community for some help.

ifstream myIF;
    myIF.open("QBInfo.txt");
    string trash;
    string temp;
    getline(myIF, trash);
    for(int i = 0; i < kNumPlayers; i++){
        getline(myIF, temp);
        players[i].name = temp;
        for(int j = 0; i < kNumGames; j++){
            myIF >> players[i].completions[j];
            myIF >> players[i].attempts[j];
            myIF >> players[i].yards[j];
            myIF >> players[i].touchdowns[j];
            myIF >> players[i].interceptions[j];
        }
    }

Here is the test case file that my instructor has provided. The first number is for a challenge portion of the assignment that I will be attempting later due to time restrictions. It is the reason for the getline(myIF, trash) portion of code before my loop begins.

3
Peyton Manning              
27  42  462 7   0
30  43  307 2   0
32  37  374 3   0
28  34  327 4   0
33  42  414 4   1
28  42  295 2   1
29  49  386 3   1
30  44  354 4   3
25  36  330 4   0
24  40  323 1   0
Tom Brady               
29  52  288 2   1
19  39  185 1   0
25  36  225 2   1
20  31  316 2   0
18  38  197 0   1
25  43  269 1   1
22  46  228 0   1
13  22  116 1   1
23  33  432 4   0
29  40  296 1   1
Drew Brees              
26  35  357 2   1
26  46  322 1   2
29  46  342 3   1
30  39  413 4   0
29  35  288 2   0
17  36  236 2   1
26  34  332 5   0
30  51  382 2   2
34  41  392 4   0
30  43  305 1   1

解决方案

Use istringstream to populate the structure members with the data read from the file. Naming may not be particularly important in a short program such as this - but it's a good habit to think about variable names that convey meaning. The data file appears to have the number of players in the first line. getline was used to read past this line. Normally that would be used to initialise a number-of-players variable.

std::ifstream infile;
infile.open("qb.dat");
std::string nPlayers;
std::string playerName;

getline(infile, nPlayers);
getline(infile, playerName);

for (int player = 0; player < kNumPlayers; ++player)
{
  for (int game = 0; game < kNumGames; ++game)
  {
    std::string dataRow;
    getline(infile, dataRow);
    std::istringstream is(dataRow);
    is >> players[player].completions[game] >>
          players[player].attempts[game] >>
          players[player].yards[game] >>
          players[player].touchdowns[game] >>
          players[player].interceptions[game];
  }
}

这篇关于从文件中读取到结构的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆