【问题标题】:Why is my getline() not reading the first line of my standard input in C?为什么我的 getline() 没有读取 C 中标准输入的第一行?
【发布时间】:2020-04-18 21:39:52
【问题描述】:

我对 C 比较陌生,我正在使用 getline() 函数来读取作为标准输入提供的文件的内容。

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

对于上下文:文件读取-

a b c -d e 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);    

【问题讨论】:

  • 您正在泄漏正在读取的第一行。您应该使用do..while 循环而不是while 循环。您还泄漏了每个 duplicate 令牌。
  • @nicksheen 不,它没有。阅读getline 的文档。
  • @JosephSible-ReinstateMonica 你是对的,对不起。我刚看了第一段

标签: c getline strtok


【解决方案1】:

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

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

while 循环再次运行 getline 并忽略第一次运行的结果。

【讨论】:

    【解决方案2】:

    在进入 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 1970-01-01
      • 2021-08-23
      • 2020-04-05
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多