Hello friends. I am experiencing problems using the getline function in C. I want to use the function to print line numbers next to the text of each line of an input file. I have successfully read in the file and printed its contents line by line. But when I add the part to print the line numbers using getline, the compiler reports an error and the code doesnt run.
Here is the error I am getting
[Linker error] undefined reference to `getline'
ld returned 1 exit status
Here is the code I am using:
#include <stdio.h>
#include <string.h>
# define NEWLINE '\n'
int main()
{
static const char filename[] = "Bond.in.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char line [ 200 ];
while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
{
fputs ( line, stdout ); /* write the line */
}
/*********** code that gives problem ***********/
int counter = 0;
while ( getline(&filename, &line, stdin) )
{
++counter;
printf( "%d , %d", counter, line);
}
/************* end of code that gives problem****/
fclose ( file );
}
else
{
perror ( filename );
}
char wait;
scanf( "%c", &wait );
return(0);
}
I dont understand where this error is coming from. I tried several varaitions using different parameters for getline but all resulted in the aformentioned error. It is my understanding that getline is a built in function included in the stdio library. I havent been able to test my code to see if it is correct because of this error.
Thanks for any help.