简体   繁体   English

无法在C中为struct分配正确的值

[英]Having trouble assigning the correct values to struct in C

I am trying to read information off of a text file and use the info to assign values to my struct variables. 我正在尝试从文本文件中读取信息,并使用该信息将值分配给我的结构变量。 My text file is as follows: 我的文本文件如下:

A 2 B C  
B 1 D NULL  
C 0 NULL NULL  
D 0 NULL NULL

My source code is as follows: 我的源代码如下:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct tree_node
{
    char *node_name;    
    int children_no;
    char *child_Name1;
    char *child_Name2;
};

struct tree_node nodes[4];

int main()
{
    char nodeName[10];
    int noChildren;
    char childNames1[10];
    char childNames2[10];       
    int i = 0;

    FILE *f = fopen("inputFile.txt", "r");
    if(f == NULL)
    {
        fprintf(stderr, "Can't open input file.\n");
        exit(1);
    }
    while (fscanf(f, "%s %d %s %s", nodeName, &noChildren, childNames1,childNames2) != EOF)
    {
        nodes[i].node_name = nodeName;
        nodes[i].children_no = noChildren;
        nodes[i].child_Name1 = childNames1;
        nodes[i].child_Name2 = childNames2;
        printf("Node name = %s; Number of children = %d; child 1 = %s; child 2 = %s\n", nodes[i].node_name, nodes[i].children_no, nodes[i].child_Name1, nodes[i].child_Name2);
        i = i+1;
    }

    printf("INNCORECT PRINT:\n");
    for(int j = 0; j<4; j=j+1)
    {
        printf("Node name = %s; Number of children = %d; child 1 = %s; child 2 = %s\n", nodes[j].node_name, nodes[j].children_no, nodes[j].child_Name1, nodes[j].child_Name2); 
    }
}

The printf inside the while loop prints correctly, but when I try to print outside the while loop in the for loop below, it seems all 4 elements of my struct array are the last input from the text file. while循环内的printf可以正确打印,但是当我尝试在下面的for循环中的while循环外打印时,似乎struct数组的所有4个元素都是文本文件的最后一个输入。 The console output is as follows: 控制台输出如下:

Node name = A; Number of children = 2; child 1 = B; child 2 = C
Node name = B; Number of children = 1; child 1 = D; child 2 = NULL
Node name = C; Number of children = 0; child 1 = NULL; child 2 = NULL
Node name = D; Number of children = 0; child 1 = NULL; child 2 = NULL
INNCORECT PRINT:
Node name = D; Number of children = 2; child 1 = NULL; child 2 = NULL
Node name = D; Number of children = 1; child 1 = NULL; child 2 = NULL
Node name = D; Number of children = 0; child 1 = NULL; child 2 = NULL
Node name = D; Number of children = 0; child 1 = NULL; child 2 = NULL

Any help at all on why this is happening would be greatly appreciated, as I have been stuck with this for a while now. 非常感谢所有关于为什么会发生这种情况的帮助,因为我已经坚持了一段时间。 Apologies in advance if my question format is wrong, this is my first time posting here. 如果我的问题格式有误,请您提前道歉,这是我第一次在这里发布。 Thanks in advance! 提前致谢!

With strdup() 使用strdup()

while (fscanf(f, "%s %d %s %s", nodeName, &noChildren, childNames1,childNames2) != EOF) {
    nodes[i].node_name = strdup(nodeName);
    // should check strdup() for failure since its malloc() may fail

    nodes[i].children_no = noChildren;
    nodes[i].child_Name1 = strdup(childNames1);
    nodes[i].child_Name2 = strdup(childNames2);
    printf("Node name = %s; Number of children = %d; child 1 = %s; child 2 = %s\n", nodes[i].node_name, nodes[i].children_no, nodes[i].child_Name1, nodes[i].child_Name2);
    i = i+1;
}

With malloc() and strcpy() 使用malloc()和strcpy()

while (fscanf(f, "%s %d %s %s", nodeName, &noChildren, childNames1,childNames2) != EOF) {
    nodes[i].node_name = malloc(strlen(nodeName) + 1);
    nodes[i].child_Name1 =  malloc(strlen(childNames1) + 1);
    nodes[i].child_Name2 =  malloc(strlen(childNames2) + 1);
    // be sure to check these for failure via == NULL

    strcpy(nodes[i].node_name, nodeName);
    nodes[i].children_no = noChildren;
    strcpy(nodes[i].child_Name1, childNames1);
    strcpy(nodes[i].child_Name2, childNames2);
    printf("Node name = %s; Number of children = %d; child 1 = %s; child 2 = %s\n", nodes[i].node_name, nodes[i].children_no, nodes[i].child_Name1, nodes[i].child_Name2);
    i = i+1;
}

In both cases, you should free the memory for node_name , child_Name1 and childName2 when you are finished with them. 在这两种情况下,完成后都应为node_namechild_Name1childName2释放内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM