Advertisement
Guest User

Untitled

a guest
May 4th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. char *read_file(const char *filename)
  2. {
  3. int i=0;
  4. FILE *readFile = fopen(filename, "r");
  5.  
  6. if(!readFile) return NULL;
  7. if(feof(readFile)) return NULL;
  8.  
  9. char *returnString=malloc(sizeof(char));
  10. returnString[i]=fgetc(readFile);
  11.  
  12. while(!feof(readFile)){ //Lukee merkin kerrallaan kunnes tullaan tiedoston loppuun
  13. i++;
  14. returnString = realloc(returnString, (i+1)*sizeof(char));
  15. returnString[i] = fgetc(readFile);
  16. }
  17.  
  18. i++;
  19. returnString = realloc(returnString, (i+1)*sizeof(char));
  20. returnString[i] = NULL;
  21. fclose(readFile);
  22. return returnString;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement