经过几个小时的阅读,我想我可以澄清我的问题这是一个与任务相关的问题,但我正在寻找一个概念上的帮助,而不是我的确切代码的解决方案我的代码是为了可视化而包含的。
我在一个文本文件中有以下数据,这些数据被读入我的程序:

HIS1043.002 MH2.102   MWF   1:00-1:50pm     120 35.00
GEO1013.005 MB1.101   TR   12:30-1:45pm       5 35.00
MAT1214.003 MS1.02.03 TR    2:00-3:15pm       1 35.00
CS1713.002  NPB1.202  MWF   1:00-1:50pm       0 50.00
MAT3013.001 MS1.02.07 TR    2:00-3:15pm       1 35.00
ENG1023.001 MH2.202   MWF  10:00-10:50am     15 35.00

下面是我使用的两个typedef结构:
// Course Definition
typedef struct
{
    StudentNode *pWaitlistHead;
    char szCourseId[12];
    char szRoom[15];
    char szDays[15];
    char szTimes[15];
    int  iAvailSeats;
    double dFee;
} Course;


// Node for course list
typedef struct CourseNode {
    struct CourseNode* pNext;
    Course course;
} CourseNode;

这是从文件中读取数据的函数在函数中,我调用了另外两个函数:一个为课程节点分配空间,另一个将数据添加到列表中。
int getCourses(Course courseM[])
{
    char szInputBuffer2[100];
    int i = 0;

    while(fgets(szInputBuffer2, 100, pFileCourses) != NULL)
    {
        sscanf(szInputBuffer2, "%12s  %15s %8s %15s %d %lf",
                          courseM[i].szCourseId, courseM[i].szRoom,
                          courseM[i].szDays, courseM[i].szTimes,
                          &courseM[i].iAvailSeats, &courseM[i].dFee);
    }
    //allocate space for linked-list
    pNew = allocateNodeC(courseM); //???

    //add courses to linked list
    insertN2CourseList(&pHead, &pNew);

    return i;
}

我的问题是如何处理列表实现中的课程描述数据我需要在分配中分别处理每个元素吗当从文件中读取所有数据时呢?
    //allocate space for CourseNode linked list
CourseNode *allocateNodeC(Course courseM[])
{
    CourseNode *pNew = malloc(sizeof(CourseNode));
    pNew->pNext = NULL;
    pNew->course.szCourseId = courseM[i].szCourseId; //?????
    pNew->course.szRoom = courseM[i].szRoom; //?????
    pNew->course.szDays = courseM[i].szDays; //?????
    pNew->course.szTimes = courseM[i].szTimes; //?????
    pNew->course.iAvailSeats = courseM[i].iAvailSeats; //?????
    pNew->course.dFee = courseM[i].dFee; //?????
    return pNew;
}
//add courses to linked list
CourseNode insertN2CourseList(CourseNode **ppHead, CourseNode *ppNew)
{
    CourseNode p*;
    if(*ppHead == NULL)
    {
        *ppHead = pNew;
        return;
    }
    for(p = *ppHead; p->pNext != NULL; p = p->pNext);
}

最佳答案

szCourseIdszRoomszDaysszTimes属于char []类型,用于存储从文件读取的字符串:

        sscanf(szInputBuffer2, "%12s  %15s %8s %15s %d %lf",
                      courseM[i].szCourseId, courseM[i].szRoom,
                      courseM[i].szDays, courseM[i].szTimes,
                      &courseM[i].iAvailSeats, &courseM[i].dFee);

allocateNodeC()中,您试图分配给数组:
    pNew->course.szCourseId = courseM[i].szCourseId; //?????
    pNew->course.szRoom = courseM[i].szRoom; //?????
    pNew->course.szDays = courseM[i].szDays; //?????
    pNew->course.szTimes = courseM[i].szTimes; //?????

C中,数组不可赋值相反,您应该使用courseMstrcpy()成员的内容复制到新创建的节点,如下所示:
    strcpy (pNew->course.szCourseId, courseM[i].szCourseId);
    strcpy (pNew->course.szRoom, courseM[i].szRoom);
    strcpy (pNew->course.szDays, courseM[i].szDays);
    strcpy (pNew->course.szTimes, courseM[i].szTimes);

此外,这似乎不是实际的代码,因为在函数allocateNodeC()中,如果i并且您正在访问courseM[i],则没有声明。

10-08 18:16