gpt4 book ai didi

c - 如果超过数组大小怎么办,如何打印错误信息?并终止程序

转载 作者:太空宇宙 更新时间:2023-11-04 01:10:18 28 4
gpt4 key购买 nike

使用最大大小为 50 的固定长度数组。如果输入文件包含超过 50 名学生,则打印一条消息,例如“文件包含超过 50 名学生!”并终止程序。

我不知道如何添加这个。我尝试了很多使用 if/else 语句的方法,但总是遇到 segmentation 11 问题。请帮忙

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 50

// Function declarations
void printInfo();
int readresults(FILE*,int*, int*, int*);
void sort(float avgScore[], int stuID[], int totalStu);
float calcAvg (int score1, int score2);
void getInfo(float avgScore[], int totalStu, float* min, float* max, float* avg);
void writeSortedResults (FILE* AG_Sorted, float avgScore[], int stuID[], int totalStu);
void printdone();

int main (void)
{
// Local Declarations
FILE* AG_Midterm;
FILE* AG_Sorted;

int score1[MAX_SIZE];
int score2[MAX_SIZE];
int stuID[MAX_SIZE];
int i = 0;
int totalStu = 0;

float avgScore[MAX_SIZE];
float avg;
float min;
float max;

// Statements
printInfo();

AG_Midterm = fopen("/Users/r3spectak/Desktop/AG_Midterm.txt", "r");
if(AG_Midterm == NULL)
{
printf("\aError opening Results File\n");

return 100;

} // if open input

if(!(AG_Sorted = fopen ("/Users/r3spectak/Desktop/AG_Sorted.txt","w")))
{
printf("\aError opening Average Results file\n");
return 102;
}// if open input
i = 0;


while(readresults(AG_Midterm, &stuID[i], &score1[i], &score2[i]))//Kexy
{
avgScore[i] = calcAvg(score1[i], score2[i]);
i++;//Kexy
totalStu++;//Kexy


} //while

sort(avgScore, stuID, totalStu);//Kexy

getInfo( avgScore, totalStu, &min, &max, &avg );
printf( "\nHighest Average Score: %.2f\nLowest Average Score: %.2f\nClass Average Score : %.2f\n", max, min, avg );
printf("Total Students: %d\n", totalStu);

writeSortedResults(AG_Sorted, avgScore, stuID, totalStu);

printf("Total Students: %d\n", totalStu);

fclose(AG_Midterm);
fclose(AG_Sorted);


printdone();
return 0;

} // main


/*==================printInfo==================================
Reads data from Midterm file
Pre : Nothing
Post: Prints introduction message
*/

void printInfo()
{
// Statements
printf("Begin Calculation of Scores\n");
return ;

} // printInfo


/*===================readResults==================================
Reads data from AG_Midterm file
Pre : AG_Midterm is an open file.
stuid, score1 , score2
Post: reads score1 and score2
if data read -- returns 1
if EOF or error--returns 0
*/

int readresults(FILE* AG_Midterm, int * stuID, int *score1, int *score2)
{
// Local Variables
int i;
int items;
int ioCheck;

// Statements
ioCheck = fscanf(AG_Midterm, "%d%d%d",stuID, score1, score2);

if(ioCheck == EOF)

{
return 0;
}

else if(ioCheck != 3)

{
printf("Error reading file...\n");
return 0;
}

else

return 1;

} //readresults

/*===================sort===================================
Sorts by selecting the largest Average score in unsorted
portion of array and exchanging it with element at the
beginning of the unsorted list.=
Pre : list must contain atleast one item
Post: list rearranged largest to smallest

*/

void sort(float avgScore[], int stuID[], int totalStu)
{
// Local Declarations
int i = 0;
int j = 0;
int largest;
int tempID;

float temp;

for(i = 0; i < totalStu; i++)
{
largest = i;
for(j = i + 1; j < totalStu; j++)
{
if(avgScore[j] > avgScore[largest])
largest = j;

}

temp = avgScore[i];
tempID = stuID[i];

avgScore[i] = avgScore[largest];
stuID[i] = stuID[largest];

avgScore[largest] = temp;
stuID[largest] = tempID;
}
} // sort


/*===================calcAvg==================================
Determines the Average of the two midterm scores
Pre : score1, score2
Post: avgScore copied to addresses

*/
float calcAvg (int score1, int score2)

{
return (score1 + score2)/2.0;
} // calcAvg


/*===================getInfo==================================
Determines the Class Average,Lowest Average
and Highest Average scores
Pre : avgScore , totalStu
Post: avg , min , max

*/
void getInfo(float avgScore[], int totalStu, float* min, float* max, float* avg)
{
int i;
float sum = *min = *max = avgScore[0];

for( i = 1; i <totalStu; ++i )
{
sum += avgScore[i]; /* sum = sum + dAry[i] */
if( avgScore[i] < *min ) *min = avgScore[i]; /* update ... if applies */
if( avgScore[i] > *max ) *max = avgScore[i];
}
*avg = sum/totalStu;
return;
}

/*===================writeSortedResults==================================
Writes Average Scores after Sorting
Pre : AG_Sorted is an open file
avgScore
Post: Data written to file
*/
void writeSortedResults (FILE* AG_Sorted, float avgScore[], int stuID[], int totalStu)
{
// Local Definitions
int i;
int line = 0;

// Statements
for(i = 0; i < totalStu; i++)
{
fprintf(AG_Sorted, "%d %.2f ", stuID[i], avgScore[i]);
line++;

if(line == 8)
{
fprintf(AG_Sorted, "\n");
line = 0;
}

}

printf("\nSuccessfully write the sorted data to the new file\n");
return;


} // writeSortedResults

/*==================printDone==================================
Reads data from Midterm Score File
Pre : Nothing
Post: Prints end message
*/
void printdone()
{
// Statements
printf("End of Finalizing Scores.Thank you.\n");
return;

} // printDone

最佳答案

如果我理解您的意图,您不仅对避免像每个人似乎都在修复的超限感兴趣;如果输入文件 溢出,但您实际上没有让它溢出,您正试图完全中止,因此您的进程出现段错误。检查 feof() 对您没有任何好处,因为您可能有换行符、一些空格,以及其中几乎所有不是的常规记录条目,以保持 feof() 为真。

为了解决您想要的结果(在太大的文件上中止),您可以将数据读入本地变量,只要您在大小限制内,就将它们放入您的数组然后;不是在阅读期间。否则中止。

int id, s1, s2;
while(readresults(AG_Midterm, &id, &s1, &s2))//Kexy
{
// we *know* we have a valid record. make sure it fits
// before dropping it in, otherwise proclaim the file too
// large and abort the whole thing.
if (i == MAX_SIZE)
{
printf("Too many scores. Aborting.\n");
exit(EXIT_FAILURE);
}

stuID[i] = id;
score1[i] = s1;
score2[i] = s2;

avgScore[i] = calcAvg(score1[i], score2[i]);
i++;//Kexy
totalStu++;//Kexy

} //while

关于c - 如果超过数组大小怎么办,如何打印错误信息?并终止程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13796056/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com