为什么我的 getline() 没有读取 C 中标准输入的第一行?

Why is my getline() not reading the first line of my standard input in C?

我是 C 的新手,我正在使用 getline() 函数读取作为标准输入提供的文件的内容。

但是,while 循环不读取文件的第一行,我不确定为什么!

对于上下文:文件读取-

a b c
-d
F

并且输出读取并正确拆分 -def,但仅在 while 循环之外打印 a b c

int main (){

//utilising data provided at http://www.linux.die.net/man/3/getline
//char*linePointer is initialized to NULL, for getline() to allocate a buffer that stores the line
//buffer gets resized dynamically 

char *linePointer = NULL; 
size_t len = 0;

//checks if the file is empty and prints -, if it is
if ((getline(&linePointer, &len, stdin) == -1)){
    printf("-\n");
}

//as long as the stream is valid, and the file can be read 
//if either of the conditions are not satisfied then while loop condition is not satisfied 
//prints the contents of the line 

Clauses cs = createNewArrayList();
printf("%s\n", linePointer);
while ((getline(&linePointer, &len, stdin) != -1)){
        printf("%s\n", linePointer);

    Clause c = createNewArrayList();
    char *token; 
    char *delim = " ";
    token = strtok(linePointer, delim);
    while (token != NULL){      
        char *duplicate = strdup(token);     
        add(c, duplicate);
        printf("%s\n",duplicate);
        token = strtok(NULL, delim);
    }
    add(cs, c);
}

free(linePointer);
exit(EXIT_SUCCESS);    

因为您的第一个 getline 正在使用第一行:

//checks if the file is empty and prints -, if it is
if ((getline(&linePointer, &len, stdin) == -1)){
    printf("-\n");
}

while 再次循环 运行s getline 并忽略第一个 运行.

的结果

您在进入 while 循环之前阅读并丢弃了第一行,这就是循环看不到该行的原因。

试试这个:

int main (){

    //utilising data provided at http://www.linux.die.net/man/3/getline
    //char*linePointer is initialized to NULL, for getline() to allocate a buffer that stores the line
    //buffer gets resized dynamically 

    char *linePointer = NULL; 
    size_t len = 0;

    //checks if the file is empty and prints -, if it is
    if ((getline(&linePointer, &len, stdin) == -1)){
        printf("-\n");
    }
    else{
        //as long as the stream is valid, and the file can be read 
        //if either of the conditions are not satisfied then while loop condition is not satisfied 
        //prints the contents of the line 

        Clauses cs = createNewArrayList();
        do{
            printf("%s\n", linePointer);

            Clause c = createNewArrayList();
            char *delim = " ";
            char *token = strtok(linePointer, delim);
            while (token != NULL){      
                char *duplicate = strdup(token);     
                add(c, duplicate);
                printf("%s\n",duplicate);
                //if add() makes its own copy, then uncomment this,
                //or simply don't use strdup() above to begin with:
                //free(duplicate);
                token = strtok(NULL, delim);
            }
            add(cs, c);

            free(linePointer);
            linePointer = NULL; 
            len = 0;
        }
        while (getline(&linePointer, &len, stdin) != -1);
    }

    free(linePointer);
    exit(EXIT_SUCCESS);
}