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) {
- int sym;
- char *mystring = malloc(1 * sizeof(char));
- int capacity = 1;
- int len = 0;
- if (!f) {
- return NULL;
- }
- while ((sym = fgetc(f))) {
- if (len == capacity - 1) {
- capacity *= 2;
- char* tmp_mystring = mystring;
- mystring = realloc(tmp_mystring,capacity);
- if(!mystring){
- return NULL;
- }
- }
- mystring[len] = sym;
- ++len;
- if(sym == '\n'){
- mystring[len] = '\0';
- return mystring;
- }
- }
- if(strlen(mystring) == 0){
- return NULL;
- }
- mystring[len] = '\0';
- return mystring;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement