看来我的程序有问题导致错误,并且在尝试调用 fgets 后立即卡住。我可能是错的,但这似乎停止了,所以有人知道问题是什么吗?
该文件有我的标题,其中包含定义和结构,主要用于对完整列表进行排序。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"compare.h"
#define BLOCK 2
int input_record(record *rec, record_list *list);
void list_init(record_list *list);
void list_destroy(record_list *list);
void list_print(record_list *list);
int check_args(const char arg1[], const char arg2[], record_list *list);
int main(int argc, char *argv[]){
record_list list;
record rec;
char arg1[3];
char arg2[3];
/*check for proper number of arguments*/
if(argc > 3 || argc < 1){
return 0;
}
/*run default if no switches*/
if(argc == 1){
input_record(&rec, &list);
} else {
strcpy(arg1, argv[1]);
strcpy(arg2, argv[2]);
arg1[3] = '\0';
arg2[3] = '\0';
input_record(&rec, &list);
if(check_args(arg1, arg2, &list) == 0){
printf("error");
}
}
list_print(&list);
list_destroy(&list);
return 0;
}
int input_record(record *rec, record_list *list){
int score;
int min = -1;
int max = 101;
int count = 0;
char first_name[NAMESIZE];
char last_name[NAMESIZE];
char temp[LINESIZE];
record *nrec;
list_init(list);
for(count = 0; count < NLINES; count++){
printf("Enter first name, last name, score\n");
/* !! seg fault occurs once i enter the data !! */
if(fgets(temp, LINESIZE, stdin) == 0){
break;
}
printf("222222");
if(sscanf(temp, "%s %s %d", first_name, last_name, &score) == 3){
if(strlen(first_name) >= NAMESIZE || strlen(last_name) >= NAMESIZE){
printf("error");
continue;
}
if(score < min || score > max){
printf("error");
continue;
}
}
record *tempRec;
if(list->nalloc == 0){
tempRec = malloc(sizeof(record));
if(tempRec == 0){
return 0;
}
#ifdef DEBUG
fprintf(stderr, "#\n");
#endif
list->nalloc = 1;
list->data = tempRec;
}else if(list->nalloc == list->nused){
tempRec = realloc(list->data, (list->nalloc*BLOCK) * sizeof(record));
if(tempRec == 0){
return 0;
}
list->data = tempRec;
list->nalloc *= BLOCK;
}
list->data[list->nused] = *rec;
strcpy(rec->name.first, first_name);
strcpy(rec->name.last, last_name);
rec->score = score;
list->nused++;
}
return 1;
}
void list_init(record_list *list){
list->nused = 0;
list->nalloc = 0;
list->data = 0;
}
void list_destroy(record_list *list){
free(list->data);
list->nalloc = 0;
list->nused = 0;
}
void list_print(record_list *list){
int count;
int listsize = sizeof(list);
for (count = 0; count < listsize; count++){
printf("%s %s %d", list->data[count].name.last, list->data[count].name.first, list->data[count].score);
}
}
int check_args(const char arg1[], const char arg2[], record_list *list){
char nameDown[] = "-n";
char nameUp[] = "+n";
char scoreDown[] = "-s";
char scoreUp[] = "+s";
/*if either argument is not one of the valid switches, kill function*/
if((strcmp(arg1, nameDown) != 0) || (strcmp(arg1, nameUp) != 0) || (strcmp(arg1, scoreDown) != 0) || (strcmp(arg1, scoreUp) != 0)){
return 0;
}
if((strcmp(arg2, nameDown) != 0) || (strcmp(arg2, nameUp) != 0) || (strcmp(arg2, scoreDown) != 0) || (strcmp(arg2, scoreUp) != 0)){
return 0;
}
/*if the arguments are the same, kill function*/
if (strcmp(arg1, arg2) == 0){
return 0;
/*otherwise look for all combinations of valid switches*/
} else if (strcmp(arg1, nameDown) == 0){
if (strcmp(arg2, scoreDown) == 0){
sort_desc_name_desc_score(list->data);
} else if (strcmp(arg2, scoreUp) == 0){
sort_desc_name_asc_score(list->data);
/*if same lettered switches are detected, kill function*/
} else if (strcmp(arg2, nameUp) == 0){
return 0;
} else {
/*the ps and qs are supposed to be records to be sorted*/
sort_desc_name(list->data);
}
} else if (strcmp(arg1, nameUp) == 0){
if (strcmp(arg2, scoreDown) == 0){
sort_asc_name_desc_score(list->data);
} else if (strcmp(arg2, scoreUp) == 0){
sort_asc_name_asc_score(list->data);
} else if (strcmp(arg2, nameDown) == 0){
return 0;
} else {
sort_asc_name(list->data);
}
} else if (strcmp(arg1, scoreDown) == 0){
if (strcmp(arg2, nameDown) == 0){
sort_desc_score_desc_name(list->data);
} else if (strcmp(arg2, nameUp) == 0){
sort_desc_score_asc_name(list->data);
} else if (strcmp(arg2, scoreUp) == 0){
return 0;
} else {
sort_desc_score(list->data);
}
} else if (strcmp(arg1, scoreUp) == 0){
if (strcmp(arg2, nameDown) == 0){
sort_asc_score_desc_name(list->data);
} else if (strcmp(arg2, nameUp) == 0){
sort_asc_score_asc_name(list->data);
} else if (strcmp(arg2, scoreDown) == 0){
return 0;
} else {
sort_asc_score(list->data);
}
/*and in case anything else got through*/
} else {
return 0;
}
return 1;
}
这就是整个程序,在 input_record 函数中发生了段错误。任何因为这个我还没有能够测试我的代码的其他功能......