Hi all,

First time poster here, I've referenced posts here on daniweb in the past and it has always been helpful, thanks! But I can't find the answer to this particular problem here on the forums.

I need to write a basic string parser as part of a coding assignment here at penn state. I have completed the assignment using getc, but bonus points are offered for a solution using getline.

The getline() man page (http://linux.die.net/man/3/getline) requires stdio.h and stdlib.h for getline to compile.

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

int main()
{
//welcome to main!
//we need our input values!

char inputstr[128];

while(1)
{
if(feof(stdin) != 0)
{ break; }

getline(&inputstr, 128, NULL);
//did that work!?
printf("input string is : %s\n", inputstr);
}

getc(stdin);
return 0;

}

This code wont even compile!!
Here is the compiler error and log, from Dev-C++(set for standard C)

LOG:

Compiler: Default compiler
Executing  gcc.exe...
gcc.exe "E:\pr1\proj1.c" -o "E:\pr1\proj1.exe"    -I"E:\Dev-Cpp\include"   -L"E:\Dev-Cpp\lib"
C:\DOCUME~1\xxxxxx\LOCALS~1\Temp/ccwrbaaa.o(.text+0x61):proj1.c: undefined reference to getline'
collect2: ld returned 1 exit status

Execution terminated

ERROR:

[Linker error] undefined reference to getline'
ld returned 1 exit status

Unfortunately, my go-to book, C:ARM(C a reference manual) doesnt even have this function! :(

Thanks for any help or insight you can provide!

-Tav

EDIT:

I realize that getline is not a standard C function, but even when I compile using

-std=c99

I get the same error!

Edited by taverasme: n/a

6
Contributors
8
Replies
36
Views
7 Years
Discussion Span
Last Post by Nikolas9896

Try something like below:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

char inputstr[128];
char *cptr = &inputstr[0];
size_t thesize = 128;

while(1)
{
if(feof(stdin) != 0)
{ break; }

getline(&cptr, &thesize, stdin);

printf("input string is : %s\n", inputstr);
}

getc(stdin);
return 0;

}

Or you could:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char *cptr = NULL;
size_t thesize = 0;

while(1)
{
if(feof(stdin) != 0)
{ break; }

getline(&cptr, &thesize, stdin);

printf("input string is : %s\n", cptr);

free(cptr);
cptr =  NULL;
}

getc(stdin);
return 0;

}

Edited by gerard4143: n/a

Try something like below:

With the MinGW version of gcc:

main.c
main.c: In function main':
main.c:18: warning: implicit declaration of function getline'
Debug/main.o: In function main':
C:/Users/Dave/Projects/scratch/main.c:18: undefined reference to _getline'
collect2: ld returned 1 exit status
*** Errors occurred during this build ***

I need to write a basic string parser as part of a coding assignment here at penn state. I have completed the assignment using getc, but bonus points are offered for a solution using getline.

Are they expecting you to then be using a *nux platform?

I realize that getline is not a standard C function, but even when I compile using

-std=c99

I get the same error!

Yes. It's not standard in either C90 or C99. ?

>>my go-to book, C:ARM(C a reference manual) doesnt even have this function!
because getline() is not a standard C function. Its a standard c++ function, not C. If your compiler implements a C getline() function than ok, but don't expect to read about it in any text book.

Linux man states

CONFORMING TO
Both getline() and getdelim() are GNU extensions. They are available
since libc 4.6.27.

Edited by gerard4143: n/a

Hi,i need help please,when i compile cpp file it give me error

main.cpp:158: error: 'getline' was not declared in this scope

i am newbie and i dont know what to do...i find some info about problems with getline on mac os x....the program run without problem in ubuntu but on os x i have this error.

Thanks.

First question, are you writing a C++ program or a C program? I ask because this is the C section and your error indicates a .cpp file.

Edited by gerard4143: n/a

Hello! How are can "free(cptr);" If you dosen't used to dynamic memorys?
You will helped -** man getline** - in this scope you could see how use that function.

Edited by Nikolas9896

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.