#include "txtfile.h" void strcat_mem(char **dest, char * src) { char * buf; if (*dest != NULL) { buf = (char*)calloc(strlen(*dest) + strlen(src), sizeof(char)); strcpy(buf, *dest); strcat(buf, src); free(*dest); } else { buf = (char*)calloc(strlen(src), sizeof(char)); strcat(buf, src); } *dest = buf; } char *fstrread(FILE *f) { char *line = NULL; char *buf = calloc(BUF_LEN, sizeof(char)); while (fgets(buf, BUF_LEN, f) != NULL) if (buf[strlen(buf) - 1] == '\n') { strcat_mem(&line, buf); break; } else strcat_mem(&line, buf); free(buf); strtok(line, "\n"); return line; } Main program code
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "errcode.h" #include "txtfile.h" int main(int argc, char* argv[]) { FILE *f =fopen("in.txt", "rt"); printf("%s\n", fstrread(f)); fclose(f); return ERR_OK; }