我正在写一个应该执行以下操作的程序

-open file
-read file
-tokenize file,getting name,course,grade tokens
-dynamiclly add these tokens to an array of structures


该文件具有以下格式

Khai,IE 3301,69
Ashley,MATH 1426,59
Alisaad,CSE 1325,31
August,CSE 1325,55
Ethan,CSE 1320,92


但是我不断遇到我不理解的细分错误,我看了看索引,它们似乎没有超出范围,所以我不确定发生了什么。以下是我的代码

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

struct info {
char* student;
char* courseName;
int grade;
};



int main(void)
{
    char buffer[100];
    struct info **strarray = NULL;  /*struct of Arrays*/
    char* token;
    char* studen = (char *) malloc(15);
    char* coursename = (char *) malloc(10);
    int grad = malloc(sizeof(int)),count = 0,index = 0;
    char* del = ",";
    FILE* fp = fopen("input-hw05a.csv","r");

    while(fgets(buffer,sizeof(buffer),fp) != NULL)
    {
        token = strtok(buffer,del);
        studen = token;
        while(token != NULL)
        {
            if(count == 1)
                coursename = token;
            if(count ==2)
            grad = atoi(token);

            token = strtok(NULL,del);
            count = count + 1;
        }
        /*add ONE element to the array*/
        strarray = (struct info **)realloc(strarray,(index + 1) * sizeof(struct info *));

        /*allocate memory for struct info*/
        strarray[index] = (struct info *)malloc(sizeof(struct info));

        /*copy data inso structure array*/
        strcpy(strarray[index]->student,studen);
        strcpy(strarray[index]->courseName,coursename);
        strarray[index]->grade = grad;
        index = index + 1;
    }
    int i = 0;
    for(i = 0; i < index; i++)
    {
        printf("%s %s %d\n",strarray[i]->student,strarray[index]->courseName,strarray[index]->grade);
    }

}

最佳答案

好吧,您的代码中可能不止malloc错误。我在评论中提到了它。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct info {
    char* student;
    char* courseName;
    int grade;
};

int main()
{
    char buffer[100];
    struct info **strarray = NULL;  /*struct of Arrays*/
    char* token;
    char* studen = (char *) malloc(15);
    char* coursename = (char *) malloc(10);
    int grad, count = 0,index = 0;
    char* del = ",";
    FILE* fp = fopen("input-hw05a.csv", "r");

while(fgets(buffer,sizeof(buffer),fp) != NULL)
{
    token = strtok(buffer,del);
    studen = token;
    count = 0;    /* You missed it. Otherwise, count will never be 1 and 2 again when control enters the loop second time */
    while(token != NULL)
    {
        if(count == 1)
            coursename = token;
        if(count ==2)
        grad = atoi(token);

        token = strtok(NULL,del);
        count = count + 1;
    }
    /*add ONE element to the array*/
    strarray = (struct info **)realloc(strarray,(count + 1) * sizeof(struct info *));
    /*allocate memory for struct info*/
    strarray[index] = (struct info *)malloc(sizeof(struct info));
    /*copy data inso structure array*/
    strarray[index]->student = (char *)malloc(strlen(studen)*sizeof(char));  //you need to allocate memory for student, to do a string copy
    strcpy(strarray[index]->student,studen);
    strarray[index]->courseName = (char *)malloc(strlen(coursename)*sizeof(char)); // similarly, you need to allocate memory for coursename, to do a string copy
    strcpy(strarray[index]->courseName,coursename);
    strarray[index]->grade = grad;
    index = index + 1;
}
int i = 0;
for(i = 0; i < index; i++)
{
    printf("%s %s %d\n",strarray[i]->student,strarray[i]->courseName,strarray[i]->grade);  //In your code it was strarray[index]->courseName & strarray[index]->grade
}
return 0;
}

10-08 11:04