gpt4 book ai didi

c - 在 C 中为结构分配正确的值时遇到问题

转载 作者:行者123 更新时间:2023-11-30 16:35:05 25 4
gpt4 key购买 nike

我正在尝试从文本文件中读取信息并使用该信息为我的结构变量赋值。我的文本文件如下:

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

我的源代码如下:

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

while 循环内的 printf 打印正确,但是当我尝试在下面的 for 循环中的 while 循环之外打印时,似乎我的结构体数组的所有 4 个元素都是文本文件的最后一个输入。控制台输出如下:

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

任何关于为什么会发生这种情况的帮助都将不胜感激,因为我已经被这个问题困扰了一段时间了。如果我的问题格式错误,请提前道歉,这是我第一次在这里发帖。提前致谢!

最佳答案

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

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

在这两种情况下,您都应该在使用完 node_namechild_Name1childName2 后释放它们的内存。

关于c - 在 C 中为结构分配正确的值时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com