Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- char *getline2(FILE *f) {
- if(!f){
- return NULL;
- }
- unsigned char *mystring = malloc(1 * sizeof(unsigned char));
- int capacity = 1;
- if (fgets(mystring, capacity + 1, f) != NULL){
- puts(mystring);
- capacity *= 2;
- }
- while (fgets(mystring + capacity - 1 , capacity + 1, f) != NULL) {
- puts(mystring);
- capacity *= 2;
- unsigned char *tmp_mystring = malloc(capacity * sizeof(unsigned char));
- strncpy(tmp_mystring, mystring, strlen(mystring));
- mystring = malloc(capacity * sizeof(unsigned char));
- strncpy(mystring, tmp_mystring, strlen(tmp_mystring));
- }
- return mystring;
- }
- int main() {
- FILE *pFile;
- pFile = fopen("myfile.txt", "r");
- char* str = getline2(pFile);
- // puts(str);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement