从一个文件读入结构数组 [英] Reading from a file into an array of structs

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

问题描述

首先,我想说,这是一个为我的CS161类的任务,所以虽然直接的答案是不错的,一个很好的解释会帮助我比任何东西。我们本周已经介绍了结构,但我遇到了一些我的代码的麻烦。我现在的目标是从一个名为QBInfo.txt的文件读取一个三个结构的数组。

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

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

此外,如果有帮助,我应该说kNumPlayers是一个常量设置为3,kNumGames是一个常数设置为10.



这是我用来从我的文件读取的代码。它似乎无法正常工作,但我一直在亏损为什么,所以我想我可能会要求社区的一些帮助。

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

这是我的教师提供的测试用例文件。第一个数字用于作业的挑战部分,由于时间限制,我将尝试稍后。这是在循环开始之前 getline(myIF,trash)部分代码的原因。

  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
汤姆·布雷迪
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
c> 使用 istringstream 用从文件读取的数据填充结构成员。
在这样的短程序中,命名可能不是特别重要 - 但是考虑传递意义的变量名是一个好习惯。
数据文件在第一行显示有玩家数。 getline用于读取此行。通常用于初始化玩家数量变量。

  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);
是>> player [player] .completions [game]>>
players [player] .attempts [game]>>
players [player] .yards [game]>>
玩家[player] .touchdowns [game]>>
players [player] .interceptions [game];
}
}


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天全站免登陆