c - strtok() 在解析过程中返回 NULL

标签 c strtok

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

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

所以我使用带有 2 个分隔符 ,(空格和逗号)的 strtok() 函数 但由于某种原因,当我到达 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);
}

所以当文件名应该是 6 时,我得到 NULL

有了这个输入:

file1: file2,file3,file4

我应该在哪里得到 fileNamefile2 我正在得到 '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;
}

最佳答案

#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() reads in at most one less than size characters from stream

你的例子:

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

关于c - strtok() 在解析过程中返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39038304/

相关文章:

c - strtok 未按预期工作,仅适用于前几次迭代

c - 地址越界 - 在访问 strtok 返回的指针时

c - LibUSB 驱动程序问题 : timeout

c - C程序的奇怪行为

c - 将输入文件拆分为单独的数字,然后将它们用作程序的输入

c - Trouble\0 null 终止字符串 (C)

c - strtok() 在 C 中给出段错误

c - 如果我们将打印结构变量本身,程序的行为会是什么

将 matlab 函数转换为 C

c - minGW 中的线程 C 程序