Advertisement
wermington

fgetline

Jan 10th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. int fgetline(FILE * f, char ** line){
  2. if(f == NULL) return -1;
  3. if(line == NULL) return -2;
  4. unsigned long alloc = 32;
  5. *line = malloc(sizeof(char) * (alloc + 1));
  6. char c = 0;
  7. size_t i = 0;
  8. while((c = fgetc(f)) != '\n' && c != EOF){
  9. (*line)[i++] = c;
  10. if((i+2) >= alloc){
  11. alloc *= 2;
  12. *line = realloc(*line, (alloc + 1) * sizeof(char));
  13. if(*line == NULL) return -4;
  14. }
  15. }
  16. (*line)[i] = '\0';
  17.  
  18. return c == EOF;
  19.  
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement