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.

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.

I just looked this up in Linux and for Linux you have to include this line

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

Thanks but I'm not using Linux. I'm using the Dev compiler and have it saved as a .c file.
UPDATE
I wrote a user defined function and I am getting the same undefined reference error when I try to call this function.

Thanks but I'm not using Linux. I'm using the Dev compiler and have it saved as a .c file.
UPDATE
I wrote a user defined function and I am getting the same undefined reference error when I try to call this function.

Dumb question...Has this compiler worked before? i.e.have you successfully compiled other programs with this setup.

Dumb question...Has this compiler worked before? i.e.have you successfully compiled other programs with this setup.

Yes I have successfully compiled with the current setup. Thats why I am so perplexed right now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.