cstack / db_tutorial

Writing a sqlite clone from scratch in C
https://cstack.github.io/db_tutorial
MIT License
9.56k stars 968 forks source link

How do you fix errors about the getline function #101

Open goldfishgggg opened 1 year ago

goldfishgggg commented 1 year ago

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

Mark-Walen commented 1 year ago

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.

wtyfpc commented 1 year ago

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;

}`