So I'm trying to separate the words from an input file and put them into an array. However, when I try to fit them into my array during a loop, I later try to print my array and it only returns NULL for each item. Here's what I got:
所以我试图将输入文件中的单词分开并将它们放入数组中。但是,当我在循环期间尝试将它们放入我的数组时,我稍后尝试打印我的数组,它只为每个项目返回NULL。这是我得到的:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main (void)
{
FILE *inputfile;
int table_size = 100;
char *table[table_size];
inputfile = fopen("input.txt", "r");
char str[60];
char *token;
int i = 0;
if (inputfile == NULL)
{
printf("Could not open file!");
return 1;
}
while ( fgets (str, 60, inputfile)!=NULL )
{
token = strtok(str, " ");
while (token != NULL)
{
token = table[i];
//strcpy(table[i], token);
i++;
token = strtok(NULL, " ");
}
}
int x;
for (x=0; x <= sizeof(table)/sizeof(int); x++)
{
printf("%s \n", table[x]);
}
fclose(inputfile);
return 2;
}
I know the while loop separates the file into words, but for some reason I can't manage to get those words into the array properly. I have a commented out strcpy function in there because I feel like that function could help a lot, but it isn't working properly either when I try to implement it.
我知道while循环将文件分成单词,但由于某种原因,我无法正确地将这些单词放入数组中。我在那里有一个注释掉的strcpy函数,因为我觉得这个函数可以帮助很多,但是当我尝试实现它时它也无法正常工作。
I'm not sure if the problem is with memory allocation or what. I'm a beginner and transitioning from python to C, so I'm not used to memory allocation, pointers, and the like. Thanks in advance.
我不确定问题是关于内存分配还是什么问题。我是初学者,从python转换到C,所以我不习惯内存分配,指针等。提前致谢。
1 个解决方案
#1
2
Right now you have a two-dimensional array with an unknown amount of elements in table. You can use strdup
to dynamically allocate memory for the element.
现在你有一个二维数组,表中包含未知数量的元素。您可以使用strdup为元素动态分配内存。
while (fgets (str, 60, inputfile)!=NULL) {
token = strtok(str, " ");
while (token != NULL)
{
table[i] = strdup(token);
i++;
token = strtok(NULL, " ");
}
}
Also your code for printing the elements is not correct because sizeof
can't determine the amount of elements in a dynamically allocated block of memory. Instead you can iterate from 0 to i (which will be the size of table)
您的打印元素的代码也不正确,因为sizeof无法确定动态分配的内存块中的元素数量。相反,你可以从0迭代到i(这将是表的大小)
int x;
for (x=0; x < i; x++)
{
printf("%s \n", table[x]);
free(input[x]);
}
Because of the fact strdup
allocates memory you need to free it when you're done (after your call to printf
).
由于strdup分配内存这一事实,你需要在完成后释放它(在你调用printf之后)。
#1
2
Right now you have a two-dimensional array with an unknown amount of elements in table. You can use strdup
to dynamically allocate memory for the element.
现在你有一个二维数组,表中包含未知数量的元素。您可以使用strdup为元素动态分配内存。
while (fgets (str, 60, inputfile)!=NULL) {
token = strtok(str, " ");
while (token != NULL)
{
table[i] = strdup(token);
i++;
token = strtok(NULL, " ");
}
}
Also your code for printing the elements is not correct because sizeof
can't determine the amount of elements in a dynamically allocated block of memory. Instead you can iterate from 0 to i (which will be the size of table)
您的打印元素的代码也不正确,因为sizeof无法确定动态分配的内存块中的元素数量。相反,你可以从0迭代到i(这将是表的大小)
int x;
for (x=0; x < i; x++)
{
printf("%s \n", table[x]);
free(input[x]);
}
Because of the fact strdup
allocates memory you need to free it when you're done (after your call to printf
).
由于strdup分配内存这一事实,你需要在完成后释放它(在你调用printf之后)。