【问题标题】:Reading a file in C and store data in arrays在 C 中读取文件并将数据存储在数组中
【发布时间】:2013-09-20 19:44:28
【问题描述】:

我下面的代码读取 C 中的文件。它显示文件、平均分数、最高分数以及所有获得最高分数的学生的姓名。考试成绩(0-100 格式到小数点后 1 位,并使用列的字段宽度)存储在一个数组中,名称(姓名和姓氏限制为 15 个字符)存储在一个二维字符数组中,即与分数数组平行。我的问题是:

1)代码没有正确读取(打印)文件(我认为与 fscanf 和数组有关)。

2) 我的两个函数不打印结果。

感谢您的任何建议。

#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]);
        }
    }
}

【问题讨论】:

    标签: c arrays visual-studio-2010 readfile


    【解决方案1】:

    首先:scanf("%79[^/n]",fileName); 应该是scanf("%79[^\n]",fileName);,最好使用fgets()。

    第二个拼写错误:= 在if() 条件下拼错了==

     if(num[k]=hgPl){ //It finds who has the highest score
     //       ^ = wrong 
    

    应该是:

     if(num[k] == hgPl){ //It finds who has the highest score
    

    编辑:

    while 循环出错..

    fscanf(dataFile, "(%lg,%s)", &score[k], &name[k]);
    //                ^   ^  ^  remove      ^
    

    应该是:

    fscanf(dataFile, "%lg%14s", &score[k], name[k]);
    

    并在 while 循环中增加 k。在printf("%6.1f %s\n", score[k], name[k]); 之后。

    【讨论】:

    • 感谢您的回复@Grijesh。我进行了更改,但是当我运行程序时,它会像无限循环一样打印。
    • @Cesar 它输出了什么吗?在你输入一些值之后它会进入无限循环吗?
    • @Cesar btw bye(); 是什么?
    • @Cesar 它不在无限循环中,但我猜它在 fscanf() 函数中的结构,因为您在格式化字符串中使用了不必要的字符:阅读更新的答案编辑部分。要了解它,请阅读:Store data in array from input
    • 输入路径名文件后,显示一长串不寻常的字符,一串串快速打印(无限循环)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2013-11-19
    相关资源
    最近更新 更多