C:列表,结构:错误:结构没有成员
英语不是我的母语,我很抱歉在描述或代码中有语法错误,我翻译它是为了在这里与大家分享 您好,我正在用C写一个小程序,我需要一些帮助,我遇到了一个无法修复的错误,我在论坛和其他任何地方搜索过,但没有找到任何帮助。程序中的其他功能工作正常 此函数从txt文件中读取单词和类别列表,将其放入结构中,生成列表。用户输入他想从文件中删除的单词,所以函数会搜索是否有,并删除是否有 我不是列表高手,所以这里可能有一个非常基本、愚蠢的问题,有什么帮助吗C:列表,结构:错误:结构没有成员,c,file,list,structure,member,C,File,List,Structure,Member,英语不是我的母语,我很抱歉在描述或代码中有语法错误,我翻译它是为了在这里与大家分享 您好,我正在用C写一个小程序,我需要一些帮助,我遇到了一个无法修复的错误,我在论坛和其他任何地方搜索过,但没有找到任何帮助。程序中的其他功能工作正常 此函数从txt文件中读取单词和类别列表,将其放入结构中,生成列表。用户输入他想从文件中删除的单词,所以函数会搜索是否有,并删除是否有 我不是列表高手,所以这里可能有一个非常基本、愚蠢的问题,有什么帮助吗 void REMOVE_WORD (int howmanyli
void REMOVE_WORD (int howmanylines)
{
FILE *fp;
if ((fp=fopen("words.txt", "r+w"))==NULL)
{printf("Error while opening the file!\n");
exit(1);}
else
{
typedef struct base
{
struct base *next;
char word[25];
char category[15];
} list_els;
struct base tab[howmanylines];
int i=0;
while (!feof(fp))
{
fscanf(fp, "%s", &tab[i].word);
fscanf(fp, "%s", &tab[i].category);
i++;
}
fclose(fp);
list_els *head;
list_els *el=(list_els*)malloc(sizeof(list_els));
list_els *ind=head;
while (ind->next)
{
ind=ind->next;
ind->next=el;
}
printf("What word do you want to remove?\n\n");
char word_remove[25];
scanf("%s", &word_remove);
if (strcmp(ind->next->word, word_remove)==0)
{
printf("Found:\n Word: | Category:\n %s | %s\n", ind->next->word, ind->next->category);
printf("Are you sure you want to remove?\n1)Yes\n 2)No\n\n");
int removing;
if (removing==1)
{
list_els *removed=ind->next;
ind->next=removed->next;
free(removed);
}
else if (removing==2) {printf("Removing cancelled.\n\n");}
else {printf("Wrong operation number!");}
}
else printf("Word not available in the base!\n\n");
}
}
它向我显示了一个错误,“struct base”在我使用strcmp的行中没有名为“word”的成员。在此代码段中
您没有将
ind
初始化为任何有效值。你可能想写ind=el
而不是head
。另外,不要强制转换malloc()
我无法重现这个错误:你真的认为用typedef
定义你的结构最合适的地方是在else
中吗?再见,代码可读性…如果你真的要编译这段代码,它将会失败得惊人。让我们从基础开始:将整个文件读入数组struct base tab[]
,然后尝试将其转换为列表,但只分配一条记录,并在其偶数设置之前使用值ind->next
,然后只测试一个指针,查看它是否匹配word\u remove
。您可能达到的值有点太高。较旧版本的c不支持可变长度数组,因此您将无法定义选项卡[howmanylines]
。您使用的是哪个版本?你是用-std=c99
编译的吗?如前所述,我还是列表的初学者,我正在寻找任何提示和线索。正如我所说的,我知道会有一些基本的错误。谢谢你指出这些问题,我会在列表上做更多的工作,但是你能告诉我如何解决这个问题吗?
list_els *head;
list_els *el=(list_els*)malloc(sizeof(list_els));
list_els *ind=head;
while (ind->next)
{
ind=ind->next;
ind->next=el;
}