db_tutorial icon indicating copy to clipboard operation
db_tutorial copied to clipboard

How do you fix errors about the getline function

Open goldfishgggg opened this issue 2 years ago • 3 comments

implicit declaration of function 'getline'; did you mean 'getenv'? [-Wimplicit-function-declaration]gcc

goldfishgggg avatar May 26 '23 07:05 goldfishgggg

implicit declaration of function 'getline'; did you mean 'getenv'? [-Wimplicit-function-declaration]gcc

"I encountered this issue on Windows 10 and found that this function may not work on Windows. So, I tried running the same code under WSL and it worked successfully.

Mark-Walen avatar May 30 '23 10:05 Mark-Walen

You can try to implement this function by yourself on windows. `ssize_t getline(char **linep, size_t *n, FILE *fp) { int ch; size_t i = 0; if (!linep || !n || !fp) { return -1; } if (*linep == NULL) { if (NULL == (*linep = malloc(*n = 128))) { *n = 0; return -1; } }

while ((ch = fgetc(fp)) != EOF)
{
    if (ch == '\n')
        break;
    if (i + 1 >= *n)
    {
        char *temp = realloc(*linep, *n + 128);
        if (!temp)
        {
            return -1;
        }
        *n += 128;
        *linep = temp;
    }
    (*linep)[i++] = ch;
}
(*linep)[i] = '\0';

return !i && ch == EOF ? -1 : i;

}`

wtyfpc avatar Jun 09 '23 07:06 wtyfpc