Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you fix errors about the getline function #101

Open
goldfishgggg opened this issue May 26, 2023 · 2 comments
Open

How do you fix errors about the getline function #101

goldfishgggg opened this issue May 26, 2023 · 2 comments

Comments

@goldfishgggg
Copy link

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

@Mark-Walen
Copy link

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
Copy link

wtyfpc commented Jun 9, 2023

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;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants