我在 C 中创建了这段代码,以逐行读取文本文件并将每一行存储到数组的位置:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static const int MAX_NUMBER_OF_LINES = 100000;
static const char filename[] = "file.csv";
int main ( void )
{
// Read file and fill the Array
char *myArray[MAX_NUMBER_OF_LINES];
int numberLine = 0;
FILE *file = fopen (filename, "r");
if (file != NULL)
{
char line [128];
while (fgets (line, sizeof line, file) != NULL)
{
myArray[numberLine] = line;
numberLine++;
}
fclose (file);
}
// Print the Array
for (int i = 0; i<numberLine; i++)
{
printf("%d|%s", i, myArray[i]);
}
}
但是在打印数组时,它是空的。我究竟做错了什么?