Skip to content

Instantly share code, notes, and snippets.

@lawrencejones
Created November 13, 2013 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawrencejones/7454243 to your computer and use it in GitHub Desktop.
Save lawrencejones/7454243 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#define MAX_BUFF_SIZE 256
int getaline(char* linebuf, int bufsize)
{
int i = -1; char c;
// While i is less than buffersize
// and the next character does not equal \n
while ((++i < bufsize - 1) && ((c = getchar()) != '\n'))
{
// Copy newly retrieved character
linebuf[i] = c;
}
linebuf[i] = '\0';
return 1;
}
int main(int argc, char**argv)
{
char *linebuffer = malloc(sizeof(MAX_BUFF_SIZE));
getaline(linebuffer, MAX_BUFF_SIZE);
printf("The linebuffer now contains: %s\n",linebuffer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment