Put the whole file into an array:
Code:
#include <stdio.h>
#include <stdlib.h>
#define LLEN 256
char **readfile(char *file, int *i) {
FILE *fh = fopen(file,"r");
char **lines = malloc(sizeof(char*));
*i = 0;
lines[0] = malloc(LLEN);
while (fgets(lines[*i],LLEN,fh)) {
(*i)++;
lines = realloc(lines,((*i)+1)*sizeof(char*));
lines[*i] = malloc(LLEN);
}
fclose(fh);
return lines;
}
int main(int argc, char *argv[]) {
int len, i;
char **lines;
if (!argv[1]) puts("No file!");
lines=readfile(argv[1],&len);
for (i=0;i<len;i++) printf("%s",lines[i]);
return 0;
}