600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > c语言结构体数组放入文件中 c-从文件中读取数据并存储到结构数组中

c语言结构体数组放入文件中 c-从文件中读取数据并存储到结构数组中

时间:2019-11-20 08:09:11

相关推荐

c语言结构体数组放入文件中 c-从文件中读取数据并存储到结构数组中

因此,我需要帮助创建一个程序来打开文件,并将文件中的数据读取到结构数组中,然后计算各种东西,例如最高,最低,平均和标准偏差.现在,我更关心如何读取实际文件并将其放入结构数组中.

以下是分配的说明:

-您将从输入文件scores.txt中读取输入数据(将发布在

练习曲);并且数据采用以下格式:(学生ID,名字,姓氏,exam1,

考试2和考试3).

-一个学生的每一行数据都将从文件中读取,然后分配给

结构变量.因此,您将需要一个结构数组来存储所有

从输入文件读取的数据.这将是一维数组.

-一旦从文件读取数据到数组,就需要计算

并显示每次考试的以下统计信息.

这是数据文件:

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

#include

#include

#include

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;

}

}

目前,我忽略了我在注释中添加的变量.我当时正在考虑放弃一个openFile函数,而只是在main函数中这样做,但是我决定反对这样做,以使我的main外观看起来更“干净”.我考虑只是在做inFile>>在我调用openFile函数后使用了arr [],但似乎不太可能起作用或有意义.

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。