-1

I have include<stdio.h>, why it show that [Error] 'getline' was not declared in this scope ?

what should I do? Thank you!

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void read_input(InputBuffer* input_buffer) {
    
    ssize_t bytes_read = 
                      getline(&(input_buffer->buffer), &(input_buffer->buffer_length), stdin);//this line show error

    if (bytes_read <= 0) {
        printf("Error reading input\n");
        exit(EXIT_FAILURE);
    }

    input_buffer->input_length = bytes_read - 1;
    input_buffer->buffer[bytes_read - 1] = 0;
}
  • Are you running this on a POSIX-compliant system? – mediocrevegetable1 May 29 '21 at 14:07
  • 1
    "Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008." – Irelia May 29 '21 at 14:07
  • I am a beginner and do not what is POSIX. I am following a blog to build an easy database, then I meet this error. I code on win10, not on Linux, my problem is on this? – 雨泉清音 May 29 '21 at 14:27
  • @雨泉清音 Try copying [this program](https://onlinegdb.com/c9fKb6BbDq) into your IDE/editor and try compiling it. If it gives an error, `getline` is not supported on your system. – mediocrevegetable1 May 29 '21 at 14:29
  • @mediocrevegetable1 it actually gives an error, so I need to change to work on Linux? – 雨泉清音 May 29 '21 at 14:32
  • @雨泉清音 if you can then sure. However, there are standard alternatives to `getline`. You can create a large buffer size from before and use `fgets`. If no characters were read, a `NULL` pointer will be returned. So you can check for an error with that. – mediocrevegetable1 May 29 '21 at 14:36
  • 1
    @mediocrevegetable1 very very thank you! – 雨泉清音 May 29 '21 at 14:41

1 Answers1

0

man getline says that it is defined under _POSIX_C_SOURCE >= 200809L since glibc 2.10 and _GNU_SOURCE before. So try to add #define _POSIX_C_SOURCE 200809L before all your includes in the beginning. If that doesn't help, also add #define _GNU_SOURCE. But this works for glibc only. If you are on Windows/MSVC, then use fgets(), just have large enough buffer to fit any line with ending newline and null terminator.

ivan.ukr
  • 2,853
  • 1
  • 23
  • 41
  • This will not work. How can a function be defined simply from a macro being defined? The real purpose of these macros is to *check the version* to see if certain features/functions exist, not to define them yourself. – mediocrevegetable1 May 29 '21 at 18:20
  • Such macros affect what header files define. So you need to define one of such macros to have header file give you a definition. – ivan.ukr May 29 '21 at 18:23
  • Actually, maybe you're right. According to [this question](https://stackoverflow.com/questions/5582211/what-does-define-gnu-source-imply), defining `_GNU_SOURCE` provides extra features (though this would probably not work for a compiler like MSVC). I'm still not sure about `_POSIX_C_SOURCE` though. I believe that is a macro that should only be defined by the implementation to show that it is POSIX-compliant, I don't think the programmer is supposed to define it themselves. Besides, since OP is saying they're using WIndows, I doubt `#define _POSIX_C_SOURCE` will have any effect at all. – mediocrevegetable1 May 29 '21 at 18:31
  • yes, `_GNU_SOURCE` and all stuff behind it is glibc specific. So it won't work for the MSVC. – ivan.ukr May 29 '21 at 18:33
  • " since OP is saying they're using Windows" - missed that. Since `getline()` is GNU extension, then for MSVC it is not available at all. So then use `fgets()`. – ivan.ukr May 29 '21 at 18:37
  • 1
    It seems like I was wrong about `_POSIX_C_SOURCE` too, and that [you can define it with a certain value to get more features](https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html) :P I'm not sure if that applies for mingw as well though (if OP is using mingw. they may be using MSVC too, in which case both macros would be pretty useless). – mediocrevegetable1 May 29 '21 at 18:44
  • 1
    mingw uses msvcrt, so unlikely it is available. – ivan.ukr May 29 '21 at 18:46