我正在尝试从文本文件中读取信息并使用info为我的struct变量赋值。我的文本文件如下:
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循环外打印时,我的struct数组的所有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
对于为什么会发生这种情况的任何帮助都会非常感激,因为我已经坚持了一段时间了。如果我的问题格式错误,请提前道歉,这是我第一次在这里发帖。提前谢谢!
答案 0 :(得分:0)
使用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_name
,child_Name1
和childName2
的内存。