Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  5.  
  6. char *getline2(FILE *f){
  7.  
  8. if (f = NULL){
  9. return NULL;
  10. }
  11. int len = 1;
  12. int now = 0;
  13. char *str = (char*)malloc(sizeof(char) * len);
  14. int c;
  15. for(c = getchar(); (c != EOF) && (c != '\n'); c = getchar()){
  16. str += c;
  17. now++;
  18. if (now == len){
  19. len *= 2;
  20. str = (char *) realloc (str, len);
  21. }
  22. }
  23. if (c == '\n'){
  24. now++;
  25. str += '\n';
  26. if (now == len){
  27. len *= 2;
  28. str = (char *) realloc (str, len);
  29. }
  30. }
  31. now++;
  32. str += '\0';
  33. fclose (f);
  34. return str;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement