Created
November 13, 2013 18:49
-
-
Save lawrencejones/7454243 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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