将数据从文件输出到结构和结构数组 [英] Outputting Data From A File Into Structs And Arrays Of Structs

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

问题描述

我有很多麻烦,能够从一个文件获取数据,并将其输入到给定的结构和数组的结构,然后输出该文件。我们得到一个包含96行的文件,如下所示:

I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:


Arzin,Neil

2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6

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

Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5

此文件继续为24个不同的人,然后重复与不同的分数(第二行)。
第一个数字,在这种情况下是2.3,两个人都是难度评级。接下来的6个数字是分数。

This file continues for 24 different people and then repeats with different scores (the second line). The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.

为了设置结构和数组以及我的代码,我们给出了这些数据:

We are given this data in order to set up our structs and arrays and my code:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()
{

  ifstream inFile;
  inFile.open("C://diveData.txt");

  // checking to see if file opens correctly
  if (!inFile)
  {
      cout << "Error opening the file!" << endl;
  }

  const int numRounds = 2;              // variable to hold the number of rounds
  const int numScores = 7;              // variable to hold the number of rounds
  const int numDivers = 24;             // variable to hold the number of divers
  typedef double DifficultyList[numRounds];   // 1D array for storing difficulty                of dives on each round 
  typedef double ScoreTable [numRounds] [numScores]; // 2D array of dive scores

// struct to store information for one diver
  struct DiverRecord
{
   string name;
   double totalScore;
  double diveTotal;
  DifficultyList diff;
  ScoreTable scores;
};

DiverRecord DiverList[numDivers];

// my attempt at printing out the contents of the file
  while (!EOF)
 {
    for (int x = 0; x < 25; x++)
    { 
       infile >> DiverList[x].name;
       inFile >> DiverList[x].totalScore;
       inFile >> DiverList[x].diveTotal;

       cout << DiverList.[x].name << endl;
       cout << DiverList.[x].totalScore << endl;
       cout << DiverList.[x].diveTotal << endl;
    }
  }

return 0;
}


推荐答案

评论我现在正试图集中在不使用structs和数组的结构,我试图使用其他函数,以从文件中读取数据。我想把数据以逻辑形式,如:

As I said in my earlier comment I am now trying to concentrate on not using the structs and arrays of structs and am trying to use other functions in order to read the data from the file. I want to put the data in a logical form such as:

人的名字
回合1:难度分数
回合2:难度分数

Name Of Person Round 1: Difficulty Scores Round 2: Difficulty Scores

但我无法访问文件中的特定元素。

But I am having trouble accessing specific elements from the file.

我使用的代码是:

while(inFile)
{
   string name;
   getline(inFile, name);
   cout << name << endl;
}

这样会输出数据文件,但是当我尝试声明不同的变量对于难度和七分,它不能正确输出。

This outputs the data file as is, but when I try to declare different variables for the difficulty and the seven scores it does not output correctly at all.

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

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