I have created a struct in which I am trying to store whole text from file. The problem is that pointers and memory allocation does not want to collaborate with me.
typedef struct Text{
char **line;
} Text;
After many variations, at this time, readFile function looks like this: It prints only the first line from file (there are 4 lines).
#define LINE_LENGTH 255
void readFile(FILE *inputPtr, char *inputFileName, Text *textInFile, unsigned lineIndex) {
while(!feof(inputPtr)) {
++lineIndex;
textInFile->line = realloc(textInFile->line, LINE_LENGTH * lineIndex * sizeof(char));
fgets(textInFile->line[lineIndex], LINE_LENGTH, inputPtr;
//testing
printf(textInFile->line[lineIndex]);
}
}
I would be very thankful if you could help, how this function should look?
I have tried many different *
and ()
placements, but could not make it work.
If **line
in struct is replaced with
line[100][255]
and mem. alloc. line is removed, then everything works well.