【发布时间】:2019-05-11 15:14:27
【问题描述】:
我正在尝试将一些文本放入一个结构中,我的数组的一部分是一个包含部分文本的数组。
例如我的结构是:
struct animal
{
char animal_Type[11];
int age;
int numberOfLegs;
int walksPerDay;
char favoriteFood[];
};
然后我将输入如下内容:
dog,2,4,2,biscuits,wet
cat,5,4,0,biscuits,wet,dry,whiskers
bird,1,2,0,birdseed,biscuits,bread,oats,worms,insects,crackers
我有一个可行的解决方案,可以将每天最多步行的所有值放入结构中,但是我希望能够将食物放入最喜欢的食物中。我有一个动态数组,但我不确定如何将剩余的文本读入到 favoriteFood 数组中。
使用的代码是:
fp = open("animals.txt","r");
struct animal *animal = malloc(sizeof(sturct animal)*3);
int i = 0;
if(fp != NULL) {
while(i < 3) {
fscanf(fp,"%s %d %d %d %s",
animal[i].animal_Type,
animal[i].age,
animal[i].numberOfLegs,
animal[i].walksPerDay,
animal[i].favoriteFood); // need to be able to enter the string of food into here
i++
}
我该怎么做呢?
【问题讨论】:
-
是
favouriteFood意味着包含“wet,dry,whiskers”还是包含“wet”、“dry”、“whiskers”的数组? -
啊,我的道歉,应该说清楚。 wet,dry,whiskers 都是数组中的单独文本,所以 "wet","dry","whiskers"