为什么我的代码在我标记它时会返回(null)?

时间:2013-08-06 15:38:15

标签: null linked-list segmentation-fault structure strtok

我收到了一个要阅读的文件,并将内容存储在链接列表中。

文件schedule.csv以逗号分隔,并包含:

CSE1325.001,1,0,1,0,1,10:00,11:00
CSE1325.002,0,1,0,1,0,12:30,14:00
CSE2312.001,0,1,0,1,0,14:00,15:30
CSE2315.001,1,0,1,0,1,09:00,10:00
CSE2315.002,0,1,0,1,0,12:30,14:00
ENGL1301.004,0,1,0,1,0,11:00,12:30
HIST1311.001,0,0,0,0,1,13:00,16:00
MATH1426.005,1,0,1,0,0,16:00,17:30

每一行包括一个课程编号,5 - 1或0(星期一至星期五,1表示班级见面,0表示班级不符合),接着是两次军事时间,表明班级开始时和它结束了。

到目前为止,我有:

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

struct node
{ 
        char course[15];
        int mon;
        int tue;
        int wed;
        int thu;
        int fri;
        char start[10];
        char stop[10];
        struct node *next;
};

typedef struct node link;

link *addque(char *course, char *mon, char *tue, char *wed, char *thu,
char *fri, char *start, char *stop);


int main(void)
{
        FILE *fp;
        char *schedule = "schedule.csv";
        char buffer[15];
        char *course, *del = ",";
        char *mon, *tue, *wed, *thu, *fri;
        char *start, *stop;
        link *head = NULL, *temp, *tail;

        if((fp = fopen(schedule, "r")) == NULL)
        {
                printf("unable to open %s\n", schedule);
                exit(1);
        }

        while( fgets(buffer, sizeof(buffer), fp) != NULL)
        {

/*---PROBLEM IS HERE!!!!---*/

                course = strtok(buffer, del);
                mon = strtok(NULL, del);
                tue = strtok(del, NULL);  
                wed = strtok(NULL, del);
                thu = strtok(NULL, del);
                fri = strtok(NULL, del);
                start = strtok(NULL, del);
                stop = strtok(NULL, del);

/*---PROBLEM IS HERE!!!!---*/

                printf("\n\n%s,%s,%s,%s,%s,%s,%s,%s\n\n", course, mon,
tue, wed, thu, fri, start, stop);

                    temp = addque(course, mon, tue, wed, thu, fri,
    start, stop);
                    if(head == NULL)
                            head = temp;
                    else
                            tail->next = temp;

                tail = temp;
        }
        fclose( fp );
}

link *addque(char *course, char *mon, char *tue, char *wed, char *thu,
char *fri, char *start, char *stop)
/**********************************************
        name:   addque
       input:   char *, 5x int *, 2x char *, course, days, times
      output:   struct node *, pointer to new node
*/
{
        link *temp = malloc( sizeof(link) );
        strcpy(temp->course, course);
        strcpy(temp->start, start);
        strcpy(temp->stop, stop);
        temp->mon = atoi(mon);
        temp->tue = atoi(tue);
        temp->wed = atoi(wed);
        temp->thu = atoi(thu);
        temp->fri = atoi(fri);
        temp->next = NULL;

        return temp;
} 

在我对字符串进行标记后,我将其打印以检查值。 strtok之后的printf将不在最终代码中。

无论如何,我的输出是:

CSE1325.001,1,(null),(null),(null),(null),(null),(null)

Segmentation fault

也很好奇Segmentation故障的来源。这似乎经常发生在我身上。什么是分段错误,我该如何避免呢?它是上述(null)问题的一部分吗?

1 个答案:

答案 0 :(得分:0)

突出的一个直接问题是:

course = strtok(buffer, del);
mon = strtok(NULL, del);
tue = strtok(del, NULL);        // <<<<

您获取星期二值的strtok参数是错误的。

然而,即使 你修复了它,你也不会阅读每一条完整的行,原因如下:

char buffer[15];
:
while( fgets(buffer, sizeof(buffer), fp) != NULL)
:

while循环将读取完整的一行,因为buffer的大小只有十五。您需要使它至少与最长行一样大,再加上新行和字符串终止符的几个字节。