从文件获取数据并将元素输出到表中

时间:2013-04-17 16:16:21

标签: c++ visual-c++

我获得了一个包含24人体育数据的数据文件。该活动有两轮。文件的前48行给出第1轮的数据,第48轮给出第2轮的数据。该文件如下所示:

Azrin,Neil 2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6

巴贝奇,查尔斯 2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

伯克斯,亚瑟 2.8 4.5 5.4 6.6 7.7 8.8 5.6 6.5

克拉克,弗朗西斯 1.6 6.5 6.5 6.5 7.3 7.5 7.8 5.6

居里,玛丽 2.9 9.4 9.5 9.2 8.0 5.6 9.0 9.8

戴尔,内尔 1.0 2.3 2.3 2.3 2.3 3.4 3.4 3.4

爱因斯坦,艾伯特 1.5 5.0 5.0 5.0 5.0 5.0 5.0 5.0

富兰克林,罗莎琳 2.0 7.8 7.8 7.6 7.5 8.0 6.7 7.6

海森堡,维尔纳 2.2 8.2 8.1 8.0 8.5 8.6 8.3 9.0

霍夫施塔特,道格拉斯 1.8 9.3 9.0 9.2 8.4 7.8 8.0 5.6

凯莉,多萝西 3.0 7.6 7.8 8.7 5.9 6.4 7.1 8.0

米拉德,玛格丽特 2.7 8.0 8.0 7.8 6.5 7.4 8.2 5.9

明斯基,马文 2.7 7.8 6.8 7.1 7.4 5.6 4.9 5.7

牛顿,艾萨克 2.3 9.0 9.1 9.0 9.2 9.2 9.0 8.9

奥尔森,凯瑟琳 2.6 7.8 9.1 8.4 8.4 8.2 8.4 7.6

帕斯卡,布莱斯 3.0 4.5 6.4 8.0 5.6 7.5 6.5 8.0

Pylyshyn,Zenon 2.8 8.6 7.9 9.0 6.9 7.3 6.9 7.0

萨尔,卡尔 2.5 7.8 9.0 8.7 8.6 4.6 7.6 8.0

施罗丁格,欧文 2.7 8.0 7.8 8.0 9.0 6.7 8.7 6.5

图灵,艾伦 2.5 7.8 8.0 8.0 7.9 7.3 7.6 7.6

范艾伦,詹姆斯 2.7 6.9 7.0 7.0 7.2 6.7 7.0 8.0

冯诺依曼,约翰 3.0 7.8 8.9 9.0 8.7 9.0 7.9 9.2

沃森,詹姆斯 2.8 9.6 9.7 9.7 9.0 9.5 9.7 9.0

Weems,Chip 2.5 7.8 7.8 7.6 7.5 7.8 8.0 6.7

Azrin,Neil 2.1 6.0 5.0 6.5 7.8 5.6 8.8 7.6

巴贝奇,查尔斯 2.3 5.4 6.5 7.6 8.7 7.2 5.4 5.5

伯克斯,亚瑟 2.7 4.1 5.4 5.6 7.8 8.2 5.6 6.5

克拉克,弗朗西斯 1.5 7.5 6.4 6.5 7.2 7.5 7.5 6.6

居里,玛丽 2.8 9.5 9.1 9.2 8.4 5.6 9.3 9.7

戴尔,内尔 2.0 3.3 3.3 3.3 2.3 3.1 3.0 4.4

爱因斯坦,艾伯特 2.5 5.1 5.2 5.3 5.4 5.5 5.6 5.7

富兰克林,罗莎琳 3.0 7.8 7.7 7.1 7.5 8.2 6.7 7.6

海森堡,维尔纳 2.5 8.2 8.4 8.0 8.7 8.7 8.1 9.0

霍夫施塔特,道格拉斯 2.8 9.1 9.0 9.1 8.4 6.8 8.0 6.6

凯莉,多萝西 3.0 7.4 7.1 7.7 6.9 6.4 7.1 8.0

米拉德,玛格丽特 2.8 8.1 8.0 7.1 6.5 6.4 8.2 5.9

明斯基,马文 2.1 7.8 7.8 7.1 7.4 7.6 5.9 5.7

牛顿,艾萨克 2.3 8.0 9.0 9.0 9.2 8.2 9.0 8.9

奥尔森,凯瑟琳 2.0 6.8 9.1 8.4 8.0 8.2 8.4 7.6

帕斯卡,布莱斯 3.0 5.5 6.4 8.1 5.6 7.4 6.5 7.0

Pylyshyn,Zenon 2.7 8.6 6.9 9.0 6.5 7.3 8.9 7.0

萨尔,卡尔 3.0 6.8 9.0 8.2 8.6 5.6 7.6 8.0

施罗丁格,欧文 1.7 8.0 6.8 8.5 9.0 7.7 8.7 6.5

图灵,艾伦 2.4 7.7 8.1 8.2 7.3 7.3 7.6 7.6

范艾伦,詹姆斯 2.7 7.9 7.0 6.0 7.2 8.7 7.0 8.0

冯诺依曼,约翰 3.0 7.1 8.9 9.0 7.7 9.0 8.9 9.2

沃森,詹姆斯 2.1 8.6 9.8 9.8 9.0 9.1 9.7 9.0

Weems,Chip 2.7 6.8 7.3 7.6 6.5 7.8 8.0 6.2

此数据对所有24个人继续,然后在第二轮中以​​不同的分数值重复。人名之后的第一个数字是难度级别,然后接下来的7个数字是分数。 我试图从文件中获取此数据并按以下格式进行设置:

人名 第1轮:难度分数 第2轮:难度分数

因此上述数据的表格如下所示:

Arzin,Neil

第1轮:2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6

第2轮:2.1 6.0 5.0 6.5 7.8 5.6 8.8 7.6

巴贝奇,查尔斯

第1轮:2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

第2轮:2.3 5.4 6.5 7.6 8.7 7.2 5.4 5.5

所有24个人等等。这是我的代码,但它只是直接读取文件:

// I have already opened the file!
while (inFile)
{
   string name;
   getline(inFile,name);
   cout << name << endl;
}

是否有人可以提供一些有关如何对此数据进行某种格式化的见解?

修改

我获得了以下数据结构

const int numRounds = 2;
const int numScores = 7;
const int numDivers = 24;

// 1D array for storing difficulty of dives on each round
typedef double DifficultyList[numRounds];

// 2D array of dive scores
typedef double ScoreTable[numRounds][numScores];

// struct to store info for one diver 
struct DiverRecord
{
    string name; // persons name
    double totalScore, // total score for both rounds
        diveTotal; // total on current round
    DifficultyList diff; // difficulty on all dives (1X2 array)
    ScoreTable scores; // scores for all rounds (2X7 array)
};

// array of structs of diver records
typedef DiverRecord DiverList [numDivers]; 

谢谢!

2 个答案:

答案 0 :(得分:0)

这是关于格式化的输入。

如果您真的更喜欢C ++,请查看std::ifstream的方向。有关常规流信息,请参阅this tutorial;有关格式设置,请参阅this one

此处question similar to your还有很多有用的帖子。

我通常使用C标准文件输入和fscanf()函数(man 3 fscanf)来执行此类任务。我的编码速度更快。

答案 1 :(得分:0)

您的格式是基于行的,因此第一项任务是读取行

string line;
// read lines until EOF
while (getline(inFile, line))
{
}

一旦你有了这条线,下一个任务就是解析每一行以提取你想要的数据,这样做的简单方法(对于这个任务应该足够好)就是使用istringstream。

#include <string>
#include <fstream>
#include <sstream>

string line;
// read lines until EOF
while (getline(inFile, line))
{
    // put the line into an istringstream
    istringstream buf(line);
    // read the surname and first name from the istringstream
    string surname, first_name;
    buf >> surname >> first_name;
    // read the scores until end of the line
    double score;
    while (buf >> score)
    {
        ...
    }
}

无论如何,我还不完全确定你的文件格式。

希望这有帮助。

相关问题