C:意外行为

时间:2015-04-14 00:34:28

标签: c strtok

我正在使用以下函数逐行读取文件并将这些行存储在链接列表中。文件格式为“file1:file2:file3:file4:file5:file6:”。我有一个问题,在读取file3 strtok后返回NULL并且程序退出并且其他文件无法读取。任何建议都表示赞赏。

void * readerThread(void *args) {

    char * tmp = malloc(strlen(args)+1);
    char * stuff;
    strcpy(tmp,args);
    char * newline = strchr(tmp,'\n');
    if (newline)
        *newline = '\0';
    fprintf(stderr, "All files %s", tmp);
    stuff = strtok(tmp, " :\r\n");

    while(stuff != NULL) {
        char * fileName = stuff;
        FILE * fp;
        char * line = NULL;
        size_t len = 0;
        ssize_t read;
        fp = fopen(fileName, "r");

        if (fp == NULL) {
            fprintf(stderr, "Unable to open file %s.\n", stuff);
            goto nextFile;
        }

        while((read = getline(&line, &len, fp)) != -1) {
           //Store the line
        }
    nextFile:
        stuff = strtok(NULL," :\r\n");
        fclose(fp);
        fprintf(stderr, "Starting %s\n", stuff);
    }
done:
    reading = 0;
}

0 个答案:

没有答案