論理エラーについて質問があります。エラーは
「実行時チェックの失敗 #2 - 変数 'list' の周りのスタックが壊れていました。」
in.txt ファイルには合計 60 行あります
これは私のコードです:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FILE_NAME 20
#define LIST_SIZE 50
//void getData(RECORD name[], RECORD score)
typedef struct
{
char *name;
int score;
}RECORD;
int main (void)
{
// Declarations
FILE *fp;
char fileName[FILE_NAME];
RECORD list[LIST_SIZE];
char buffer[100];
int count = 0;
int i;
// Statements
printf("Enter the file name: ");
gets(fileName);
fp = fopen(fileName, "r");
if(fp == NULL)
printf("Error cannot open the file!\n");
while(fgets(buffer, 100, fp) != NULL)
{
list[count].name = (char*) calloc(strlen(buffer), sizeof(RECORD*));
if(count > 50)
{
}
if( count < 50)
{
sscanf(buffer,"%[^,], %d", list[count].name, &list[count].score);
for( i =1; i < (50 - count); i++)
{
list[count + i].name = 0;
list[count + i].score = 0;
}
}
count++;
}
printf("Read in %d data records\n", count);
fclose(fp);
return 0;
}
このプログラムでは、ファイルから構造体の配列にデータを読み込もうとしているので、データ数が 50 未満の場合、データを持たない構造体はゼロになり、データ数が 50 を超える場合は、プログラムは最初の 50 個の構造のみを読み取ります。
実行時エラーを修正するにはどうすればよいですか?