下面是我的代碼 (C語言)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *input_file, *output_file;
int total_people, giving_to_counter, accepting, having[9];
char giver[9][13], receiver[9][13], person;
int people_scaner, people_judger;
int main () {
input_file=fopen("gift1.in", "r");
output_file=fopen("gift1.out", "w+");
fscanf(input_file, "%d\n", &total_people);
people_scaner = 0;
while(people_scaner<total_people){
people_scaner ++;
}
fscanf(input_file, "%s", &person);
printf("%s\n", &person);
people_judger = 0;
do{
people_judger ++;
} while (strcmp(&person, giver[people_judger]) !=0);
fscanf(input_file, "%d%d", &having[people_judger], &giving_to_counter);
printf("%d\n%s\n%d\n%d\n", people_judger, giver[people_judger], having[people_judger], giving_to_counter);
fclose(input_file);
fclose(output_file);
return 0;
}
下面是要讀取的文件
5
dave
laura
owen
vick
arm
dave //這一行除了dave會遇到bus error 10以外,別的人名都不會有此問題
200 3
laura
owen
vick
owen
500 1
dave
arm
150 2
vick
owen
laura
0 2
arm
vick
vick
0 0
非常感謝您的回答:)
來自stackoverflow的答案,一開定義了 char person,這是錯誤的,應該定義成char person[13]