C语言中使用结构体和指针创建学生记录

标签 c

我在尝试建立学生记录时遇到问题。 struct 有四个变量,分别是 idnameageGPA 。我应该读入一个文件并为新的学生结构动态分配内存,该结构应该保存数据集(学生记录)。最后,我应该打印从文件成功创建的学生总数、平均年龄(作为 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 variable students, but that doesn't change the value in the function that calls this one.

Carla Ramirez asked :

so using a double pointer in makeStudent function will allow me to access that array inside main method for example? How would the syntax look in C language to access that array in makeStudent?

最简单的方法是重命名参数(我选择 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/

相关文章:

`recvfrom` 函数可以用来检查之前是否有数据发送到套接字?还是需要积极倾听?

c - C 语言中的帕斯卡三角形及其组合

c - 是否可以使用整数作为数组的名称?

java - FindClass ("..","[I") 是做什么的?

c - 如何将结构与二维数组和动态内存分配一起使用

c++ - 什么时候不在头文件中使用包含保护?

C- 在删除函数中的 free() 期间或之后崩溃

c - 在给定汇编代码的情况下查找丢失的 C 代码?

c - Oracle OCI 触发器创建

c - 为什么我首先必须在 strcat() 之前使用 strcpy()?