1

我对 C++ 很陌生,所以这段代码基本上是用 g++ 编译的 C。当我在 Windows 上编译它时它可以工作,但它在 Linux 上进行核心转储。谁能看到我做错了什么?

这是更多代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "student.h"

#define MAX_LINE_LENGTH 200
#define DELIMITER ','

struct _node
{
    StudentRecord student;
    struct _node  *next;
};
typedef struct _node Node;

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: demo studentdata_2.txt\n");
        return 1;
    }

    FILE* fp = fopen("studentdata_2.txt", "r");
    if (!fp)
    {
        fprintf(stderr, "Cannot read file\n");
        return 2;
    }

    char  line[MAX_LINE_LENGTH + 1];

    Node *head = 0; 
    Node *latest;

    fgets(line, MAX_LINE_LENGTH, fp);
    printf("reaches here\n");
    while (!feof(fp))
    {
        if (strlen(line) >= 3)
        {

            Node *current = (Node*)malloc(sizeof(Node));
            current->next = 0;
            if (!head) 
                head = current;
            else
                latest->next = current;
            latest = current;

            parseStudent(line, DELIMITER, &current->student);
        }

        fgets(line, MAX_LINE_LENGTH, fp);
    }
    fclose(fp);
    printf("doesn't reach here\n");

    int count = 0;
    float sum = 0;
    Node* ptr = head;
    while (ptr)
    {
        count++;
        sum += ptr->student.m_score;
        ptr = ptr->next;
    }
    float average = sum / count;

    ptr = head;


    ptr = head;
    while (ptr)
    {
        releaseStudent(&ptr->student);

        Node* nxt = ptr->next;

        free(ptr);

        ptr = nxt;
    }

    return 0;
}
4

0 回答 0