Advertisement
Art_Uspen

Untitled

Jan 11th, 2022
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4.  
  5. char *getline2(FILE *f) {
  6.     if(!f){
  7.         return NULL;
  8.     }
  9.     unsigned char *mystring = malloc(1 * sizeof(unsigned char));
  10.     int capacity = 1;
  11.     if (fgets(mystring, capacity + 1, f) != NULL){
  12.         puts(mystring);
  13.         capacity *= 2;
  14.     }
  15.     while (fgets(mystring + capacity - 1 , capacity + 1, f) != NULL) {
  16.         puts(mystring);
  17.         capacity *= 2;
  18.         unsigned char *tmp_mystring = malloc(capacity * sizeof(unsigned char));
  19.         strncpy(tmp_mystring, mystring, strlen(mystring));
  20.         mystring = malloc(capacity * sizeof(unsigned char));
  21.         strncpy(mystring, tmp_mystring, strlen(tmp_mystring));
  22.     }
  23.     return mystring;
  24. }
  25.  
  26. int main() {
  27.     FILE *pFile;
  28.  
  29.     pFile = fopen("myfile.txt", "r");
  30.     char* str = getline2(pFile);
  31. //    puts(str);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement