Where/how to get the "getline" function if it is missing from stdio.h?
Asked Answered
T

2

15

I'm writing a program in C using Code::Blocks 13.12 on Windows 8 (the C compiler is mingw32-gcc). I would like to use the "getline" function but it seems to be missing from the stdio.h. Is there any way to get it except for writing own implementation?

Targe answered 9/12, 2014 at 14:56 Comment(1)
This might help: #13113284Fireman
C
16

getline is a POSIX function, and Windows isn't POSIX, so it doesn't have some POSIX C functions available.

You'll need to roll your own. Or use one that has already been written.

Cowbane answered 9/12, 2014 at 14:59 Comment(5)
Or use fgets which is part of the C stdlib?Sutra
@Mr.Llama If you're careful, sure...Cowbane
@Cowbane Looked over your has already been written. Not a bad implementation, but needs a few corner case improvements.Flute
@chux it's not mine, but feel free to tell the author of that code (who seems to need a reason why no one writes their own code anymore). It'd be a great reality check for him/her.Cowbane
@Cowbane The author says: "15 minutes, and I haven't written C in 10 years." In addition, the author acknowledged the edge cases, but couldn't be bothered to do yet more homework. Also the author was wondering if anyone "writes code anymore," which is not the same as "needing a reason..." It's a valid question, as most code these days is garbage and bloated. If people wrote their own code at this super basic level, the state of the web would be at least an order of magnitude better than it is now. And we wouldn't have to wade through snide remarks like the "great reality check" one.Weihs
U
5

getline is not a standard C library function. Some implementations (such as gcc) provide it as an extension.

If you're compiling with gcc, you'll need to define the feature macro _GNU_SOURCE in order to make it available for your code:

#define _GNU_SOURCE
#include <stdio.h>
...
getline (...);

EDIT

Hopefully since mingw is a GNU compiler, this should be available for you on windows.

Until answered 9/12, 2014 at 15:2 Comment(3)
@rubenvb: or that, yes.Until
Unfortunately it doesn't seem to work. I couldn't even find getline in stdio.h source file.Targe
gcc does not provide functions like getline(). These functions come from your platform's libc. MinGW uses Microsoft's libc (msvcrt.dll) with MinGW's headers.Castled

© 2022 - 2024 — McMap. All rights reserved.