C:列表,结构:错误:结构没有成员

时间:2014-01-12 20:50:39

标签: c file list structure member

英语不是我的母语,我很抱歉在描述或代码中出现任何语法错误,我翻译了它以便与你分享。

您好,我正在用C编写一个小程序,我需要一些帮助,我遇到了一个我无法解决的错误,我在论坛和其他任何地方搜索过,但我找不到任何帮助。程序中的其他功能工作正常。

此函数从txt文件中读取单词和类别列表,将其放入结构中,制作列表。用户输入他想要从文件中删除的单词,因此函数会搜索它是否在那里并删除它是否存在。

我不是最好的名单,所以这里可能有一个非常基本的,愚蠢的问题,请帮忙吗?

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'的成员。

1 个答案:

答案 0 :(得分:1)

在此代码段中

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;
}

您没有将ind初始化为任何有效值。你可能想写ind = el而不是head。另外,不要投射malloc()

相关问题