我想做一个管理学生信息的程序。但由于缺乏c语言编程技巧,我不能很好地使用指针。以下是源代码:#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct{ int id; char fName[8]; char lName[8];} Student;void main(){ int size = 0; int i = 0; /* get max lines */ scanf("%d", &size); Student *students = (Student*) malloc(sizeof(Student) * size); // allocate whole table Student *pointAt = students; // pointer used to point whole table by memory address /* retrieves data from stdin */ for (i = 0; i < size; i++) { scanf("%d", &((*pointAt).id)); printf("recorded id at %d is %d\n", i, (*pointAt).id); fgets((*pointAt).fName, sizeof((*pointAt).fName), stdin); printf("recorded fName at %d is %s\n", i, (*pointAt).fName); fgets((*pointAt).lName, sizeof((*pointAt).lName), stdin); printf("recorded lName at %d is %s\n", i, (*pointAt).lName); while (getchar() != '\n') ; } free(students);}我从中读取的文本文件,名为test.txt,是:211223344 Jennifer Smith22334455 John Bob*注意,这里第一行的2用于设置动态分配结构的大小,用于:Student students=(Student)malloc(size of(Student)*size;因为只有两个students,Jennifer和John。下面是我用来测试这个的命令:a.out < test.txt这里是输出:recorded id at 0 is 11223344recorded fName at 0 is Jennifrecorded lName at 0 is er Smitrecorded id at 1 is 22334455recorded fName at 1 is John Brecorded lName at 1 is ob^ZSuspended问题是:它似乎很好地检索studentID,但不是名(fName)和姓(lName)。fName似乎也检索了空间,这是它应该避免的。由于使用“while(getchar()”,程序进入无限循环!='\n');”以清除输入缓冲区。所以我按下ctrl+z(^z)强制终止程序,您可以在我的示例输出中看到。我该怎么解决?非常感谢你。。在第一次修复后改变了源的部分:for(i=0; i<size; i++) {scanf("%d", &((*pointAt).id));printf("recorded id at %d is %d\n", i, (*pointAt).id);getchar();fgets((*pointAt).fName, sizeof((*pointAt).fName), stdin);printf("recorded fName at %d is %s\n", i, (*pointAt).fName);getchar();fgets((*pointAt).lName, sizeof((*pointAt).lName), stdin);printf("recorded lName at %d is %s\n", i, (*pointAt).lName);}第一次固定后的输出:recorded id at 0 is 11223344recorded fName at 0 is Jenniferrecorded lName at 0 is Smithrecorded id at 1 is 22334455recorded fName at 1 is John Bobrecorded lName at 1 is Smith 最佳答案 fgets完全按照你的要求去做。传递并声明8个字符的缓冲区。前一次读取是一个scanf %d,因此它在第一个非数字字符上停止,这里是空格。然后输入流被定位在Jenniffer...(注意初始空格字符!).fgets读取7个字符,并在第8个位置放置一个终止空值,该空值给出: 1 2 3 4 5 6 7 8 J e n n i f \0如何修复?停止混合和fgets…增加缓冲区的大小。由于最长的名称是Jennifer(8个字符),因此最小大小为9,以包含终止空值。同时也要避免这种可怕的黑客攻击:scanf如果文件没有被while (getchar() != '\n';终止,或者最后一个\n被\n吃掉,那么肯定会导致无限循环最后但并非最不重要的是,控制输入函数(如scanf)的返回值。此外,main需要返回一个int而不是void,malloc返回值不应该在C中强制转换。。。固定代码可能看起来像:#include <stdio.h>#include <stdlib.h>#include <string.h>typedef struct{ int id; char fName[12]; char lName[12];} Student;int main(){ int size = 0; int i = 0; /* get max lines */ int cr = scanf("%d", &size); if (cr != 1) { fprintf(stderr, "Incorrect size"); return 1; } Student *students = malloc(sizeof(Student) * size); // allocate whole table Student *pointAt = students; // pointer used to point whole table by memory address /* retrieves data from stdin */ for (i = 0; i < size; i++) { cr = scanf("%d%11s%11s", &(pointAt->id),pointAt->fName, pointAt->lName); if (cr != 3) { fprintf(stderr, "Incorrect format line %d\n", i+1); return 1; } printf("recorded id at %d is %d\n", i, (*pointAt).id); printf("recorded fName at %d is %s\n", i, (*pointAt).fName); printf("recorded lName at %d is %s\n", i, (*pointAt).lName); pointAt += 1; // point to next struct in array } free(students); return 0;} 07-24 09:46