karimsqualli96

readline

Jan 19th, 2015
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.37 KB | None | 0 0
  1. char *readline(FILE *in) {
  2.     #define MAX_SIZE    512
  3.  
  4.     int size = MAX_SIZE;
  5.     char *str = malloc(sizeof(char) * size);
  6.     char *pos;
  7.  
  8.     while(fgets(str + size - MAX_SIZE, MAX_SIZE, in)) {
  9.  
  10.         if(pos = strchr(str + size - MAX_SIZE, '\n')) {
  11.             *pos = '\0';
  12.             break;
  13.         }
  14.        
  15.         size = size + MAX_SIZE - 1;
  16.         str = realloc(str, sizeof(char) * size);
  17.     }
  18.  
  19.     return str;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment