从文件中读取并存储数据到结构的数组 [英] Read data from file and store into an array of structs

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

问题描述

所以,我需要帮助建立一个程序,它会打开一个文件,从文件中读取数据到结构的数组,然后计算像最高,最低,平均值和标准偏差各种各样的事情。现在,我更关心的是如何读取实际的文件,并将其放置到结构的数组。

下面是分配的说明:

- 您读取输入文件scores.txt输入数据(将被张贴在
练习曲);和数据格式为(studentID,姓,名,exam1,
exam2和exam3)。

- 每一个学生的数据线将会从文件中读取,然后分配给
结构体变量。因此,你需要结构的数组来存储所有
数据从输入文件中读取。这将是一个1维阵列

- 一旦你从文件到您的阵列读取数据,你需要计算
并显示每个考试下面的统计数据。

下面是数据文件:

  1234道尔顿大卫82 86 80
9138雪莉·格罗斯90 98 94
3124辛西娅·莫利87 84 82
4532阿尔伯特·罗伯茨56 89 78
5678阿梅利亚保罗90 87 65
6134萨姆森·史密斯29 65 33
7874迈克尔加勒特贝尔91 92 92
8026梅丽莎唐尼74 75 89
9893加布宇69 66 68


 的#includestdafx.h中
#包括LT&;&iostream的GT;
#包括LT&;串GT;
#包括LT&;&的fstream GT;
#包括LT&;&了iomanip GT;使用命名空间std;结构StudentData
{
    INT studentID;
    串FIRST_NAME;
    字符串姓氏;
    INT exam1;
    INT exam2;
    INT exam3;
};const int的SIZE = 20;//函数原型
无效openInputFile(ifstream的&安培;,字符串);诠释的main()
{
    //变量
    // INT最低,最高的;
    //双平均值,标准差;
    StudentData ARR [SIZE]    ifstream的INFILE;
    字符串inFileName =scores.txt;    //调用函数读取文件中的数据
    openInputFile(INFILE,inFileName);    //关闭输入文件
    inFile.close();    系统(暂停);    返回0;
}/ **
* pre-条件:
*后置条件:
* /
无效openInputFile(ifstream的&安培; INFILE,串inFileName)
{
    //打开文件
    inFile.open(inFileName);    //输入验证
    如果(!INFILE)
    {
        COUT<< 错误打开文件。 << ENDL;
        COUT<< ENDL;
        返回;
    }
}


有关的那一刻,我忽略了,我投入的意见的变量。我在想放弃的中openFile功能,只是在主函数中这样做,但我决定不,为了使我的主要看上去有点干净。我认为只是做 INFILE>> ARR [] 后,我打电话给中openFile功能,但随后似乎不太可能,这将工作或意义。


解决方案

我的建议是:


  1. 添加操作功能,从流中读取一个 StudentData 对象。

  2. 添加在 A ,而环主。在循环的每次迭代,读了 StudentData

的std :: istream的&安培;运营商的GT;>(的std :: istream的&安培;中,StudentData&安培; ST)
{
    返回(在>> st.studentID
               >> st.first_name
               >> st.last_name
               >> st.exam1
               >> st.exam2
               >> st.exam3);
}

  openInputFile(INFILE,inFileName);为size_t numItems的= 0;
而(INFILE>> ARR [numItems的])
   numItems的++;

在那结束时,你会成功读取 numItems的物品进入改编

So I need help creating a program that will open a file and read the data from the file into an array of structs and then calculate a variety of things like highest, lowest, average and standard deviation. Right now, I am more concerned about how to read the actual file and place it into an array of structs.

Here are the instructions for the assignment:

-You will read the input data from an input file scores.txt (will be posted in Etudes); and data is in the format (studentID, first name, last name, exam1, exam2 and exam3).

-Each line of data for one student will be read from file and then assigned to a struct variable. Consequently, you will need an array of structs to store all the data read from the input file. This will be a 1-dimensional array.

-Once you read data from the file to your array, you are required to calculate and display the following statistics for each exam.

Here is the data file:

1234 David Dalton 82 86 80
9138 Shirley Gross 90 98 94
3124 Cynthia Morley 87 84 82
4532 Albert Roberts 56 89 78
5678 Amelia Pauls 90 87 65
6134 Samson Smith 29 65 33
7874 Michael Garett 91 92 92
8026 Melissa Downey 74 75 89
9893 Gabe Yu 69 66 68


#include "stdafx.h"
#include <iostream> 
#include <string> 
#include <fstream>
#include <iomanip> 

using namespace std; 

struct StudentData
{
    int studentID; 
    string first_name; 
    string last_name; 
    int exam1; 
    int exam2; 
    int exam3; 
}; 

const int SIZE = 20; 

// Function prototypes
void openInputFile(ifstream &, string); 

int main()
{
    // Variables
    //int lowest, highest; 
    //double average, standardDeviation; 
    StudentData arr[SIZE]; 

    ifstream inFile; 
    string inFileName = "scores.txt"; 

    // Call function to read data in file
    openInputFile(inFile, inFileName);

    //Close input file
    inFile.close(); 

    system("PAUSE"); 

    return 0; 
}

/**
* Pre-condition: 
* Post-condition: 
*/
void openInputFile(ifstream &inFile, string inFileName)
{
    //Open the file
    inFile.open(inFileName);

    //Input validation
    if (!inFile)
    {
        cout << "Error to open file." << endl;
        cout << endl;
        return;
    }
}


For the moment, I am ignoring the variables that I put into comments. I was thinking about forgoing an openFile function and just doing it in the main function but I decided against that to make my main look a bit "cleaner". I considered just doing inFile >> arr[] after I called the openFile function but then it seemed unlikely that it would work or make sense.

解决方案

My suggestion:

  1. Add an operator function to read one StudentData object from a stream.
  2. Add a while loop in main. In each iteration of the loop, read a StudentData

std::istream& operator>>(std::istream& in, StudentData& st)
{
    return (in >> st.studentID
               >> st.first_name
               >> st.last_name
               >> st.exam1
               >> st.exam2
               >> st.exam3);
}

and in main:

openInputFile(inFile, inFileName);

size_t numItems = 0;
while ( inFile >> arr[numItems] )
   ++numItems;

At the end of that, you would have successfully read numItems items into arr.

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

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