#define BUFFER_ADD 256
struct sFileToArray
{
char ** strings;
int stringsNum;
};
int fileToArray(const char * fName, struct sFileToArray * stringsStruct)
{
FILE * fHandle = fopen(fName, "r");
int sizeOfArray = BUFFER_ADD;
char ** strings = (char **)calloc(sizeOfArray, sizeof(char *));
if (fHandle == NULL)
{
printf("Cannot open file");
return 1;
}
char ch;
int pos = 0;
int length = BUFFER_ADD;
int stringNum = 0;
strings[stringNum] = (char *)calloc(length, sizeof(char));
while ((ch = fgetc(fHandle)) != EOF)
{
if (ch != '\n')
{
if ((pos + 1) >= length)
{
length += BUFFER_ADD;
strings[stringNum] = (char *)realloc(strings[stringNum], (length * sizeof(char)));
}
strings[stringNum][pos++] = ch;
strings[stringNum][pos] = 0;
continue;
}
pos = 0;
length = BUFFER_ADD;
if ((stringNum + 1) >= sizeOfArray)
{
sizeOfArray += BUFFER_ADD;
strings = (char **)realloc(strings, (sizeOfArray * sizeof(char *)));
}
strings[++stringNum] = (char *)calloc(length, sizeof(char));
}
stringsStruct->strings = strings;
stringsStruct->stringsNum = stringNum;
return stringNum;
}