M
Matt
Hey guys. I'm currently learnign C and I've been reading on the GNU
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.
However I am having difficulty in using it as from what I can gather
it is part of the GNU C Library, for which I can't find any
installation instructions for Windows XP. Now I can appreciate that
many people who are sufficantly advanced with C will be Linux users,
but I'm certainly not either at the moment!
Therefore, how do I install the GNU C Library in Windows?
I tried using a library header I found online (http://www.koders.com/c/
fidC1D809893A833A01A1AAE575A68832643C069C07.aspx) called "getline.h",
however I got errors such as "undefined reference to `getline'" when I
compile using the GNU C Compiler. The header file getline.h is in the
same directory as the code, which is given below:
Kind Regards,
Matt
/* Load function libraries */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "getline.h"
/* Define constant text expressions */
/* Declare variables and constants */
char *my_string;
int nbytes = 100;
unsigned int bytes_read;
/* Declare functions */
/* Main function */
int main()
{
/* Get programme to read a string from the user */
printf("Please enter a sentence: ");
/* Allocate memory first */
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline(&my_string, &nbytes, stdin);
/* Now check if -1 is returned */
if (bytes_read == -1)
{
printf("\nInsufficient memory!!!");
return 1;
}
else
{
printf("\nYou typed: %s", my_string);
}
return 0;
}
website how I should be using the getline() function instead of
"unsafe" functions such as gets(); so to build good habits I wish to
use this from now on.
However I am having difficulty in using it as from what I can gather
it is part of the GNU C Library, for which I can't find any
installation instructions for Windows XP. Now I can appreciate that
many people who are sufficantly advanced with C will be Linux users,
but I'm certainly not either at the moment!
Therefore, how do I install the GNU C Library in Windows?
I tried using a library header I found online (http://www.koders.com/c/
fidC1D809893A833A01A1AAE575A68832643C069C07.aspx) called "getline.h",
however I got errors such as "undefined reference to `getline'" when I
compile using the GNU C Compiler. The header file getline.h is in the
same directory as the code, which is given below:
Kind Regards,
Matt
/* Load function libraries */
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "getline.h"
/* Define constant text expressions */
/* Declare variables and constants */
char *my_string;
int nbytes = 100;
unsigned int bytes_read;
/* Declare functions */
/* Main function */
int main()
{
/* Get programme to read a string from the user */
printf("Please enter a sentence: ");
/* Allocate memory first */
my_string = (char *) malloc (nbytes + 1);
bytes_read = getline(&my_string, &nbytes, stdin);
/* Now check if -1 is returned */
if (bytes_read == -1)
{
printf("\nInsufficient memory!!!");
return 1;
}
else
{
printf("\nYou typed: %s", my_string);
}
return 0;
}