Home > Software design >  C: getline does not read the full document
C: getline does not read the full document

Time:05-17

I need to write a program in C reading in a big XML file with getline, the problem is only 2085 of the approxemately 3 billion lines are read. In the following code the problem occurs.

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main()                                          
{
    size_t l = 0;
    char *line;

    line = NULL;
    while (getline(&line,&l,stdin)>0) {

        getline(&line,&l,stdin);
        printf("%s\n", line);
        printf("%i\n", i);

        free(line);
        line = NULL;
    }
    return 0;
}

My first goal would be to display the lines, with printf so I can work with that. Neither line from 2080 til 2090 is empty. If I change the loop-condition to >=0 about 80.000 are read in and if I comment the printf out, about 130.00 lines. But instead I get an segmentation error. Which I really don't know how to solve in this case. I am using Visual Studio Code to edit and MYSYS2 MinGW to run the Code, on Windows 11. First the getline Function couldn't be found, I resolved it by Copy-Pasting the getline Code of this page, because the #include header's seemed not to work.

CodePudding user response:

  1. You skip every second line as you read the line in the while then you read it again in the while body
  2. You have a memory leak (same reason as point 1)
int main(void)                                          
{
    size_t l = 0;
    ssize_t retVal;
    char *line;

    line = NULL;
    do 
    {
        if((retVal = getline(&line,&l,stdin)) != -1)
        {
            printf("Buff_size:%zu Line_length: %zd Line:\"%s\"\n", l, retVal, line);

        }
    }while(retVal != -1);
    free(line);
    return 0;
}
  • Related