问题描述
- C 在做USACO Greedy Gift Givers的时候遇到bus error 10
-
下面是我的代码 (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, "%dn", &total_people); people_scaner = 0; while(people_scaner<total_people){ people_scaner ++; } fscanf(input_file, "%s", &person); printf("%sn", &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("%dn%sn%dn%dn", 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]
解决方案二:
参考:http://www.cnblogs.com/liuxueyang/archive/2013/05/21/3090546.html
时间: 2024-10-30 00:23:05