作为我的C programmng课程中的作业,我正在编写一个程序来读取文件中的名称列表,然后读取另一个文件并匹配名称并为每个玩家添加统计数据。程序运行,读取,存储和显示名称(显示名称,以便我可以检查数据),然后继续读取第二个文件,但然后它崩溃并给我一个'程序已停止响应'错误。据我所知,从谷歌搜索它可能是内存溢出或腐败,但我不知道我在做什么。我从之前的任务中重新编码代码,之前从未遇到过这个问题。提前谢谢
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#define NAME_LEN 31 /*storage for single name*/
#define TEAMSIZE 15 /*creates teamsize 0-14 (15 total)*/
void readNames();
typedef struct{
char name[NAME_LEN]; /*bowlers name ie 'jones'*/
int oversLatest; /*bowlers overs, latest and overall*/
int maidensLatest; /*bowlers maidens, latest and overall*/
int runsLatest; /*bowlers runs, latest and overall*/
int wicketsLatest; /*bowlers wickets, latest and overall*/
int strikeRate; /*bowlers strike rate*/
double average; /*average */
} player_t;
FILE *input;
player_t player_arr[TEAMSIZE]; /*creates an array of player structs*/
int main(void)
{
readNames();
char line[50];
char name1[1][50];
int O, M, R, W,
i, count;
input = fopen("input2b.dat", "r");
count = 0;
while(fgets(line, 50, input) != NULL){
sscanf_s(line, "%30s %d %d %d %d", name1, &O, &M, &R, &W, 50);
for(i=0; i<TEAMSIZE; i++){
if(strcmp(name1[0], player_arr[i].name) == 0){
player_arr[i].oversLatest = O;
player_arr[i].maidensLatest = M;
player_arr[i].runsLatest = R;
player_arr[i].wicketsLatest = W;
count++;
}
if(count == 14);
break;
}
}
fclose(input);
printf_s("\n\t\tName \tO\tM\tR\tW");
return 0;
}
/* function to read input2a.dat and assign the name to the player_t structure
*/
void readNames(){
int count; /*a counter for amount of names entered*/
int c; /*temporary storage for each name*/
input = fopen("input2a.dat", "r");
if (input == NULL) /*if no file found an error is displayed*/
printf_s("\nError - No team file found.");
count = 0; /*initialises count to 0*/
/*scans each word into file as a string and assigns it to the name of a player struct
until 15 names have been read or EOF is reached*/
do{
c = fscanf(input, "%30s", player_arr[count].name);
printf_s("%s\n", player_arr[count].name);
count++;
if(count == TEAMSIZE)
break;
} while (c != EOF);
fclose(input); /*closes file*/
}