我正在尝试使用字符串和其他结构的链表的起始地址来构建结构数组。来自文件每一行的几个字符串将被填充到该数据结构中。但是,每当我回到下一行时,到目前为止我已经填写的数组和LL的所有变量都将更改为当前行中的变量。结果,数组的所有元素以及相应的链表在数组的每个元素中给出相同的结果。这是代码。

 struct node
    {
        char* nodedata;
        struct node* link;
    };

    struct  motief
    {
        char* motiefname;
        struct node* link;
    };

void add_unique_nodes(struct motief*,int,char *);
int unique_motief(struct motief*,char*);

void add_unique_nodes(struct motief* motiefs,int motief_no,char * token)
{
    struct node *temp,*r;
    r = malloc(sizeof(struct node));
    r->nodedata = token;

    //for the first node
    if(motiefs[motief_no].link == NULL)
    {
        motiefs[motief_no].link = r;
    }
    else
    {
        temp = motiefs[motief_no].link;
        while(temp->link != NULL && token != temp->nodedata)
            temp = temp->link;
        if(token != temp->nodedata)
            temp->link = r;

    }
    r->link = NULL;
}
void main()
{
    struct motief motiefs[100];

    FILE *fp, *ft;
    fp = fopen("dump.txt","r");
    ft = fopen("motief_nodes","w");

    char line[100] ={0};

    char seps[] = ",";
    char* token;

    int motief_no = 0;



    int i,j;//loop variable

    //read the database
    while(!feof(fp))
    {
        if( fgets(line, sizeof(line), fp ))
        {
            if(motief_no == 1)
                printf("for start of 2nd step %s\t",motiefs[0].motiefname);//????
            printf("for line %d\t",motief_no);
            //get the motief from each input line as a token
            token = strtok (line, seps);

            //store it in motief array
            motiefs[motief_no].motiefname = token;
            printf("%s\n",motiefs[motief_no].motiefname);
            if(motief_no == 1)
                printf("for zero %s\n",motiefs[0].motiefname);
            motiefs[motief_no].link = NULL;

            //get and store all the nodes
            while (token != NULL)
            {
                //get the node
                token = strtok (NULL, seps);
                if(token != NULL)
                    add_unique_nodes(motiefs,motief_no,token);

            }
            if(motief_no == 0)
                printf("for end of 1st step %s\n",motiefs[0].motiefname);
            motief_no++;
            if(motief_no == 2)//break at 2nd loop, at 1
            break;
        }


我是C编程新手。我找不到原因。请帮助我找出我要去的地方,以及为什么要在我的代码中将文件除指定变量之外读取到数组中。提前致谢。这是文件中要读取的几行内容。

000100110,1,95,89
000100111,1,95,87
000100110,1,95,74
000100110,1,95,51


我用下面的代码显示结构

struct node* temp;
for(j=0;j<2;j++)
{
    printf("turn %d\t",j);
    printf("%s\t",motiefs[j].motiefname);
    temp = motiefs[j].link;
    printf("%s\t",temp->nodedata);
    do
    {
        temp = temp->link;
        printf("%s\t",temp->nodedata);
    }while(temp->link != NULL);
    printf("\n");
}


它显示了以下总体结果

for line 0  000100110
   for end of 1st step 000100110
   for start of 2nd step 000100111,1,95,87
for line 1  000100111
for zero 000100111
turn 0  000100111   1   95  87

turn 1  000100111   1   95  87

最佳答案

当您读入“行”时,您将不断更改相同的存储空间。您每次需要不同的阵列时都需要分配新的内存空间。

图片“线”指的是连续100字节的特定块。您一直告诉fgets函数写入该位置,并且当您为moteifname分配“令牌”时,也一直将该位置的地址复制到您的结构中。

然后,当您更改该地址的内容时,它当然也会覆盖该结构所指向的内容!

您需要选择在每个结构中分配空间,而不是使用内部指针,或者您需要使用malloc在每次迭代中动态分配空间,然后在最后使用free()所有指针。

https://www.codingunit.com/c-tutorial-the-functions-malloc-and-free

10-05 20:50
查看更多