diff options
Diffstat (limited to 'read.c')
-rw-r--r-- | read.c | 22 |
1 files changed, 19 insertions, 3 deletions
@@ -31,10 +31,26 @@ char* empty(void) { return str; } +char* getline(size_t* n, FILE* file) { + size_t sz = 100; + *n = 0; + int c; + char* out = malloc(sizeof(char)*100); + while ( (c = fgetc(file)) != EOF ) { + if (*n == sz) out = realloc(out, sizeof(char) * (sz *= 2)); + out[*n] = c; + (*n)++; + if (c == '\n') break; + } + out[(*n)] = 0; + //(*n)++; + return realloc(out, sizeof(char) * (*n+1) ); +} + char* line(FILE* file) { // gets a line from file - char* link = NULL; - size_t n = 0; - getline(&link, &n, file); // getline stores a line of size n in link + size_t n; + char* link = getline(&n, file); + // getline stores a line of size n in link wsclean(link, n); // removes trailing whitespace if (*link == '\n') { // empty line treated as "" free(link); |