Created
October 25, 2012 08:43
-
-
Save AlexsJones/3951479 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
| char *getline(void) | |
| { | |
| char* accumulator = malloc(100); | |
| char readBuf[100]; | |
| int accumulatorSize = 100; | |
| *accumulator = '\0'; | |
| while (!feof(stdin)) | |
| { | |
| fgets(readBuf, 99, stdin); | |
| strcat(accumulator, readBuf); | |
| /* possible fencepost error here */ | |
| if (readBuf[strlen(readBuf)] != '\n') | |
| { | |
| accumulatorSize += 100; | |
| accumulator = realloc(accumulator, accumulatorSize); | |
| /* should probably check for realloc returning null */ | |
| } | |
| else | |
| break; | |
| } | |
| return accumulator; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment