[Solved] Memory at runtime


I’d suggest counting the number of lines first.

int tmp;
int linecount = 0;
FILE *fp = fopen("file.txt","r");

while ((tmp=fgetc(fp))!=EOF) {
    if (tmp=='\n') 
         ++linecount;
}

rewind(fp); // resets the stream to beginning of file

From there, you can malloc the appropriate amount of array pointers (instead of initializing a fixed number).

 char** lines;
 lines = malloc(linecount * sizeof(char*));

and use fgets as normal to then read each line into lines[i]

2

solved Memory at runtime