阅读在C和数据存储在阵列的文件 [英] Reading a file in C and store data in arrays
问题描述
我下面的code读取C.It文件显示文件,平均分,最高分,和所有谁赢得了最高分的学生的名字。考试的分数(0-100格式1位小数,并使用列的字段宽度)被存储在一个阵列和姓名(姓名和超过15个字符的姓氏)都存储在字符的2维数组,它是平行于分数阵列。我的问题是:
1)code不读(打印)文件正确(我认为FSCANF相关和阵列)。
2)我的两个函数不打印结果。
任何建议是AP preciated,谢谢。
的#include的Tools.h
#定义MAX 30 //学生的最大数量
INT computeMax(双stSco [],诠释numSt); //获取平均最高
// 得分了
无效outputBest(双NUM [],CHAR nameHg [] [15],INT hgPl,诠释totStu);诠释的main()
{
双比分[MAX];
焦炭名[MAX] [15];
字符文件名[80];
INT K,计数= 0,hgCt;
流数据文件;
旗帜();
输出(输入你想读\\ n文件的名称);
scanf函数(%79 [^ / N],文件名);
数据文件= FOPEN(文件名,R);
如果(数据文件== NULL)
{
致命(无法打开输入%S,文件名);
}
而(!的feof(数据文件))
{
的fscanf(数据文件,(LG%,%S),&安培;比分[K],和放大器;名称[K]);
的printf(%6.1F%S \\ n,得分[K],名称[K]);
算上++; //它计算有多少学生有
}
hgCt = computeMax(评分,计); //存储由发送的值
//功能
outputBest(评分,名称,hgCt,计数);
FCLOSE(数据文件);
再见();
返回0;
}INT computeMax(双stSco [],INT numSt)
{
INT K,maxScore = 0,总和= 0;
双最大= 0,平均= 0; 对于(K = 0; K< numSt; k ++)
{
总和+ = stSco [K]; //它总结所有得分
如果(stSco [K]≥最大)
{
最大= stSco [K];
maxScore = K; //存储的最大得分的索引
}
}
平均=总和/ numSt;
的printf(平均得分为%d \\ n,平均);
的printf(中最高得分为%d \\ n,最大值);
返回maxScore;
}无效outputBest(双NUM [],CHAR nameHg [] [15],INT hgPl,诠释totStu)
{
时int k;
对于(K = 0; K< totStu; k ++)
{
如果(NUM [K] = hgPl)
{//它发现谁得分最高
的printf(%s的得分最高\\ n,nameHg [K]);
}
}
}
首先: scanf函数(%79 [^ / N],文件名);
应 scanf函数(%79 [^ \\ n],文件名);
,最好使用与fgets()
。
二错字错:拼错 ==
按 =
在如果()
状态
如果(NUM [K] = hgPl){//它发现谁得分最高
// ^ =错误
应该是:
如果(NUM [K] == hgPl){//它发现谁得分最高
编辑:
错误while循环。
的fscanf(数据文件,(LG%,%S),&安培;比分[K],和放大器;名称[K]);
// ^ ^ ^ ^删除
应该是:
的fscanf(数据文件,LG%14S%,&安培;比分[K],名称[K]);
和增量 K
在while循环。在的printf(%6.1F%S \\ n,得分[K],名称[K]);
。
My code below reads a file in C.It displays the file, the average score, maximum score,and the names of all the students who earned the maximum score. The exam scores(0-100 format to 1 decimal place and use a field width of columns) are stored in an array and the names(name and last name limited to 15 characters) are stored in a 2-dimensional array of characters that is parallel to the scores array. My problems are:
1) The code doesn't read(print) the file properly (I think is related to fscanf and the arrays).
2) My two functions don't print the results.
Any suggestion is appreciated, thanks.
#include "tools.h"
#define MAX 30 // Maximum number of students
int computeMax(double stSco[], int numSt); // Gets the average and highest
// score
void outputBest(double num[], char nameHg[][15], int hgPl, int totStu);
int main()
{
double score[MAX];
char name[MAX][15];
char fileName[80];
int k, count = 0, hgCt;
stream dataFile;
banner();
printf("Type the name of file you want to read\n");
scanf("%79[^/n]", fileName);
dataFile = fopen(fileName, "r");
if (dataFile == NULL)
{
fatal("Cannot open %s for input", fileName);
}
while (!feof(dataFile))
{
fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
printf("%6.1f %s\n", score[k], name[k]);
count++; // It counts how many students there are
}
hgCt = computeMax(score, count); // Stores the value sent by the
// function
outputBest(score, name, hgCt, count);
fclose(dataFile);
bye();
return 0;
}
int computeMax(double stSco[], int numSt)
{
int k, maxScore = 0, sum = 0;
double maximum = 0, average = 0;
for (k = 0; k < numSt; k++)
{
sum += stSco[k]; // It sums all scores
if (stSco[k] > maximum)
{
maximum = stSco[k];
maxScore = k; // Stores the index of the maximum score
}
}
average = sum / numSt;
printf("The average score is %d\n", average);
printf("The maximum score is %d\n", maximum);
return maxScore;
}
void outputBest(double num[], char nameHg[][15], int hgPl, int totStu)
{
int k;
for (k = 0; k < totStu; k++)
{
if (num[k] = hgPl)
{ // It finds who has the highest score
printf("%s got the highest score\n", nameHg[k]);
}
}
}
First: scanf("%79[^/n]",fileName);
should be scanf("%79[^\n]",fileName);
, better to use fgets()
.
Second typo mistake: misspelled ==
by =
in if()
condition
if(num[k]=hgPl){ //It finds who has the highest score
// ^ = wrong
should be:
if(num[k] == hgPl){ //It finds who has the highest score
Edit:
Error in while loop..
fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
// ^ ^ ^ remove ^
should be:
fscanf(dataFile, "%lg%14s", &score[k], name[k]);
and increment k
in while loop. after printf("%6.1f %s\n", score[k], name[k]);
.
这篇关于阅读在C和数据存储在阵列的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!