-1

我正在尝试读取文件并使用指针将字符串保存在数组中,但我遇到了问题。有人可以给我建议做什么吗?

// not allowed to change these two rows
char **Lines;
Lines = (char**)malloc(sizeof(char*)*maxLines);

...

FILE *fp;
fp = fopen(fileName, "r");     // fileName already exists here
int i=0, j=0;

while(i<maxLines){
    Lines[i] = (char*)malloc(maxLength * sizeof(char)); 
    i++;
}

// No string will be longer than "maxLenght" so no buffer is used.
while(fgets(Lines[j] , maxLength, (FILE*) fp) != NULL && j < maxLines) 
{
        j++
}

我想用文件中的每个字符串填充“行”。我不断收到分段错误。谢谢!

4

1 回答 1

1

在你的第二个while循环中替换“||” 和 ”&&”。

即使在达到 maxlines 之后,这种情况下的循环也会继续执行。

于 2019-11-15T19:06:45.913 回答