c - 从 C 中的文件读取时覆盖结构数组

标签 c file data-structures linked-list

我正在尝试使用字符串和另一个结构的链接列表的起始地址构建一个结构数组。文件每一行的几个字符串将被填充到该数据结构中。但是每当我回到下一行时,到目前为止我填充的数组和 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

最佳答案

当您读入“line”时,您会不断更改相同的内存空间。每次需要不同的数组时,都需要分配新的内存空间。

将“行”想象为指向一行中 100 字节的特定 block 。您不断告诉 fgets 函数写入该位置,并且当您将“ token ”分配给 moteifname 时,还不断将该位置的地址复制到结构中。

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

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

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

关于c - 从 C 中的文件读取时覆盖结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43619484/

相关文章:

linux - 使用 Ansible 比较两个远程主机上的文件

java,评估 URL 而不是 File 的广泛使用

go - 如何在链表的给定索引处插入节点

list - 存储多重集/无序列表的节省空间的方法

c++ - 我可以基于深度优先顺序而不是宽度优先顺序为完整的树提供类似堆的连续布局吗?

c - C 中的十进制到二进制转换(8 位)

c - 将 listen() 积压设置为 0

我可以得到一些关于 C 中这个 `isPalindrome()` 函数的反馈吗?

c - 在 C 中用 void 指针修改原件

c - 在C中修改二进制文件中的一些字节