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