Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- char *getline2(FILE *f){
- if (f = NULL){
- return NULL;
- }
- int len = 1;
- int now = 0;
- char *str = (char*)malloc(sizeof(char) * len);
- int c;
- for(c = getchar(); (c != EOF) && (c != '\n'); c = getchar()){
- str += c;
- now++;
- if (now == len){
- len *= 2;
- str = (char *) realloc (str, len);
- }
- }
- if (c == '\n'){
- now++;
- str += '\n';
- if (now == len){
- len *= 2;
- str = (char *) realloc (str, len);
- }
- }
- now++;
- str += '\0';
- fclose (f);
- return str;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement