Guest User

Untitled

a guest
Jul 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. char *readline(FILE *fp) {
  2. char buff[BUFFSIZE];
  3. int len;
  4. int copylen;
  5. char *ln;
  6. char *str;
  7. char *ptr;
  8.  
  9. len = BUFFSIZE;
  10. str = malloc(len*sizeof(*str));
  11.  
  12. while(fgets(buff, BUFFSIZE, fp) != NULL) {
  13. if((ln = strchr(buff, '\n')) != NULL) {
  14. copylen = ln - BUFFSIZE;
  15. if((ptr = realloc(str, len + copylen)) == NULL) { //FAIL }
  16.  
  17. strncat(str, buff, copylen);
  18. return str;
  19. }
  20. else {
  21. len += BUFFSIZE;
  22. if((ptr = realloc(str, len)) == NULL) { //FAIL }
  23.  
  24. strcat(str, buff);
  25. }
  26. }
  27. }
Add Comment
Please, Sign In to add comment