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=='
')
++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]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…