我在尝试建立学生记录时遇到问题。 struct
有四个变量,分别是 id
、name
、age
和 GPA
。我应该读入一个文件并为新的学生结构动态分配内存,该结构应该保存数据集(学生记录)。最后,我应该打印从文件成功创建的学生总数、平均年龄(作为 float 或 double )和平均 GPA(作为 float )或双)。正如你所看到的,我非常迷失,并且非常感谢在编写这个项目时提供的任何帮助。将永远感激不尽!这是我到目前为止所拥有的:
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
int id;
char name[255];
int age;
float gpa;
}student;
int makeStudent(FILE *fpin, student *students)
{
int num_Students = 0;
int rowNum = 0;
students = (student *)malloc(sizeof(student) * 50);
if((fscanf(fpin, "%d", students->id) < 0))
{
printf("Cannot create student record from file row # %d: id is invalid", rowNum);
exit(EXIT_FAILURE);
}
if((fscanf(fpin, "%s", students->name) != 1))
{
printf("Cannot create student record from file row # %d: name is invalid", rowNum);
exit(EXIT_FAILURE);
}
if((fscanf(fpin, "%d", students->age) < 0))
{
printf("Cannot create student record from file row # %d: age is invalid", rowNum);
exit(EXIT_FAILURE);
}
fscanf(fpin, "%d", students->gpa);
if(students->gpa < 0.0 || students->gpa > 4.0)
{
printf("Cannot create student record from file row # %d: GPA is invalid", rowNum);
exit(EXIT_FAILURE);
}
num_Students++;
rowNum++;
return 1;
}
int main()
{
FILE *fpin;
char name[255];
int size = 0;
fpin = fopen("student.txt", "r");
student studentRecord[255];
while(fgets(name, 255, fpin) != NULL)
printf("%s", name);
/*while(1)
if(!makeStudent(fpin, &studentRecord[size]))
break;
++size;
if(fpin == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
*/
fclose(fpin);
return 0;
}
以下文件数据:
1
Bob Smith
24
3.5
2
Jill Williams
23
3.6
3
Tom Jones
32
2.4
4
Julie Jackson
21
3.1
5
Al Brown
23
3.35
6
Juan Garcia
22
3.4
-7
Melissa Davis
20
3.2
8
Jack Black
44
1.1
最佳答案
在comment中,我说:
Note that you probably need to pass a
student **students
(a double pointer, rather than a single pointer) so that code outside this function can access the array of structures allocated inside the function. As it stands, you modify the local variablestudents
, but that doesn't change the value in the function that calls this one.
so using a double pointer in
makeStudent
function will allow me to access that array insidemain
method for example? How would the syntax look in C language to access that array inmakeStudent
?
最简单的方法是重命名参数(我选择 p_students
)并创建一个局部变量 student *students
代替参数。然后将代码编写为:
int makeStudent(FILE *fpin, student **p_students)
{
int num_Students = 0;
int rowNum = 0;
student *students = (student *)malloc(sizeof(student) * 50);
if (students == 0)
return 0;
*p_students = students;
其余代码可以使用students
就像以前一样。
如果您不能或不愿这样做,那么:
int makeStudent(FILE *fpin, student **students)
{
int num_Students = 0;
int rowNum = 0;
*students = (student *)malloc(sizeof(student) * 50);
if (*students == 0)
return 0;
在代码的其余部分中,引用*student
(或者,更常见的是 (*student)
)您之前只有 student
。然而,那只是为你自己的背做一根象征性的杆;让代码变得比需要的更难理解是愚蠢的。
但请注意,makeStudent()
代码需要循环最多 50 名学生。
在 main()
程序,你会写:
student *students = 0;
int n_students = makeStudent(fpin, &students);
if (n_students > 0)
{
…print them…
free(students); // Free them (let them go home at the end of the day/term/year?)
}
或者
由于您的函数是为处理一名学生而编写的,因此您可以在 main()
中进行内存分配。程序,并且不尝试 makeStudent()
中的任何内存分配。然后main()
可能看起来像:
student students[50];
for (i = 0; makeStudent(fpin, &students[i]) != 0; i++)
{
…process a student…
}
以及 makeStudent()
中的代码可能只是:
int makeStudent(FILE *fpin, student *students)
{
if ((fscanf(fpin, "%d", students->id) < 0))
{
printf("Cannot create student record from file row # %d: id is invalid", rowNum);
exit(EXIT_FAILURE);
}
…etcetera…
…除了rowNum
需要传入(在我修改后的 i
中是 main()
)...
关于C语言中使用结构体和指针创建学生记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25777772/