0
我想從一個文件讀取一個字符串列表到一個數組。 的文件時,它看起來像這樣從一個文件讀取到一個字符數組
ItemOne
ItemTwo
ItemThree etc.
我聲明數組作爲:
char** array;
和文件爲:
FILE *read;
這是我想出了:
{
i = 0;
printf("Type in the name of the file\n");
scanf("%s", &name);
read = fopen(name, "r");
if (read == NULL)
{
perror("Doesn't work");
return 1;
}
else
{
array = malloc(100 * sizeof(*array));
while (!feof(read))
{
array[i] = malloc(32 * sizeof(*array[i]));
fscanf(read, "%s", &array[i]);
i++;
}
}
}
Tt編譯,但是當我嘗試顯示它是空的數組。有任何想法嗎?
請出示您用來顯示數據的代碼。 – dasblinkenlight
你顯然已經省略了部分代碼。 –
並鑄造了'malloc()'的返回值。 – 2012-06-29 14:13:03