strtok()在解析

时间:2016-08-19 11:46:43

标签: c strtok

我尝试以这种格式解析一行:

1: 2,3,4,5,6,7,8,9,10

所以我使用strtok()函数和2个分隔符,(空格和逗号) 但由于某种原因,当我到6时,函数返回NULL。

fileName = strtok(line, spaceToken);
fileName[strlen(fileName) - 1] = 0; //remove the ':'
...
//doing something with fileName
...
fileName = strtok(NULL, commaToken);
while (fileName != NULL) <-----THE PROBLEM
    ... 
    //doing something with fileName
    fileName = strtok(NULL, commaToken);
}

因此,当fileName应为6时,我会获得NULL

使用此输入:

file1: file2,file3,file4

file2获得fileName 'fil' {}获得NULL,下一次迭代将#include <stdio.h> #include <memory.h> #define MAX_LINE_NUMBER 11 #define MAX_FILE_NAME_NUMER 255 #define MAX_FILES 10 //function declaration void parseFile(char path[]); int contain(char fileName[]); int addToDependencieArray(char fileName[], int currentFileIndex); enum COLOR { WHITE, GRAY, BLACK }; typedef struct MyFile { char name[MAX_FILE_NAME_NUMER]; int neighbors[MAX_FILES]; int neighborsCounter; enum COLOR myColor; int predecessor; } MyFile; //global MyFile gDependencies[MAX_FILES]; int gCurrentFilesWriten = 0; int main(int argc, char *argv[]) { parseFile(argv[1]); puts("hello"); } void parseFile(char path[]) { FILE *fPointer = fopen(path, "r"); char line[MAX_LINE_NUMBER]; char spaceToken[2] = " "; char commaToken[2] = ","; char *fileName; int currentFileIndex = 0; int sourseFile = 0; while (fgets(line, sizeof(line), fPointer)) { fileName = strtok(line, spaceToken); fileName[strlen(fileName) - 1] = 0; //remove the : int sourse = contain(fileName); if (sourse == -1) // isn't contains {// to create function add. currentFileIndex = addToDependencieArray(fileName, currentFileIndex); sourseFile = currentFileIndex - 1; } else // contain { sourseFile = sourse; } fileName = strtok(NULL, commaToken); while (fileName != NULL) { if (contain(fileName) == -1) { currentFileIndex = addToDependencieArray(fileName, currentFileIndex); int neighborIndex = gDependencies[sourseFile].neighborsCounter; gDependencies[sourseFile].neighbors[neighborIndex] = currentFileIndex - 1; gDependencies[sourseFile].neighborsCounter++; } fileName = strtok(NULL, commaToken); } } fclose(fPointer); } int contain(char fileName[]) { int res = -1; for (int i = 0; i < gCurrentFilesWriten; i++) { if (!strcmp(fileName, gDependencies[i].name)) { return i; } else { i++; } } return res; } int addToDependencieArray(char fileName[], int currentFileIndex) { strcpy(gDependencies[currentFileIndex].name, fileName); gCurrentFilesWriten++; gDependencies[currentFileIndex].neighborsCounter = 0; currentFileIndex++; return currentFileIndex; }

这是完整的代码,如果有帮助

304

1 个答案:

答案 0 :(得分:2)

#define MAX_LINE_NUMBER 11
...
char line[MAX_LINE_NUMBER];
...
while (fgets(line, sizeof(line), fPointer))

你只读这行的前11个字符!将MAX_LINE_NUMBER 并将其重命名增加到类似MAX_LINE_LENGTH的内容,它应该有效。

说明:when reading using fgets

  

fgets()从流

中读取最多一个 size 字符

您的示例:

123456789a|bcdef <-- character number - fgets only reads through _a_
1: 2,3,4,5|,6,7,8,9,10  <-- 5 is the last thing you read
file1: fil|e2,file3,file4 <-- "fil" is the end of the string