为什么跳过fgets()?

时间:2013-02-05 02:03:14

标签: c fgets

typedef struct {
int serial_no;
char s_name[100];
char s_street[50];
char s_town[20];
int s_speaker_no;
int max_no_teachers;
int no_applied_teachers;
} type_seminar;


void add_seminar() {
int seminar_number, temp_int;
char *temp_string;
temp_string = (char*) malloc(100 * sizeof(char));

FILE *f_sem;
FILE *f_school;
f_sem = fopen("seminars.bin", "a");
f_school = fopen("school_list.txt", "r");

fgets(temp_string, 10, f_school);
seminar_number = (atoi(temp_string) + 1);

type_seminar *temp_s = (type_seminar*) malloc(sizeof(type_seminar));
temp_s->serial_no = seminar_number;
temp_s->no_applied_teachers = 0;
temp_s->s_speaker_no = 0;

printf("Enter the seminar title: \n");
fgets(temp_string, sizeof temp_string, stdin);
strcpy(temp_s->s_name, temp_string);

printf("Enter the seminar address(street and house number): \n");
fgets(temp_string, sizeof temp_string, stdin);
strcpy(temp_s->s_street, temp_string);

printf("Enter the town (where the seminar will be held) : \n");
fgets(temp_string, sizeof temp_string, stdin);
strcpy(temp_s->s_town, temp_string);

printf("Enter the maximum number of the seminar participants : \n");
fgets(temp_string, sizeof temp_string, stdin);
temp_int = (atoi(temp_string));
temp_s->max_no_teachers = temp_int;

free(temp_s);
free(temp_string);
fclose(f_school);
fclose(f_sem);
}

每次运行该功能时,都会跳过用户输入研讨会标题的第一个fgets()。我假设从txt文件中读取的previus fgets()会在缓冲区中留下一些东西?我不知道怎么解决这个问题...而且,我是C的新手和一般的编程,所以如果它是obvius的东西......对不起:/

1 个答案:

答案 0 :(得分:3)

赞赏使用fgets来避免缓冲区溢出,但你不是完全那里:

char *temp_string;
:
temp_string = (char*) malloc(100 * sizeof(char));
:
fgets(temp_string, sizeof temp_string, stdin);

temp_string的大小是 char指针的大小,不是您分配的缓冲区的大小。这意味着您最有可能只读取四个(如果您有64位指针,则可能为八个)字符最大值,然后其余部分保留在输入流中。

你应该使用缓冲区的大小(100,尽管它更好作为定义的常量而不是硬编码的值)。

或者,看一下处理很多边缘情况的this getLine routine


而且,除此之外,您不需要乘以sizeof(char),因为根据定义,它始终保证为1 - 执行乘法只会堵塞您的源代码。

您也不应该从malloc转换返回值,因为它可以隐藏某些微妙的错误。 C能够隐式地将the void *`返回值转换为任何其他指针类型。

您需要注意的另一件事是:即使您使用fgets来保护temp_string缓冲区不会溢出,也不会为您的strcpy提供类似的保护功能

这意味着它将允许您输入一个80个字符的城镇名称,然后它会将您不应该触及的内存吹走,而不是strcpy进入20个字符的结构字段。