I'm having trouble writing a function that reads a file into an array... just like PHPs file function here file
Oh but first I cant even get the two-dimensional array to populate correctly as you can see I'm doing a test at the end of the function with a for loop.
And I really cant get this to work any way I try...any suggestions?
Code:
#include <stdio.h>
#include <stdlib.h>
#include "file.h"
char ** file(const char * filename) {
FILE *pFile;
pFile = fopen(filename, "r");
/* line count */
int lc = 0;
char **lines;
char **new_lines;
lines = (char **) calloc(1, sizeof(char));
while (!feof(pFile)) {
fgets(lines[lc], 255, pFile);
new_lines = realloc(lines, 2 * sizeof(char));
if (new_lines != NULL) {
lines = new_lines;
} else {
break;
}
lc++;
}
int i;
for (i = 0; i < 2; i++) {
printf("\n%s", lines[i]);
}
return lines;
}