gpt4 book ai didi

c - 段错误 - 文件写入

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:09 28 4
gpt4 key购买 nike

我在写入由变量 fp2 表示的文件 parent2.txt 时遇到段错误。我在程序停止运行的行和负责该问题的行中添加了注释。我猜段错误是“temp4.value”被设置为 null 的结果,尽管我以为我给了它一个值。 “temp2 = *temp2.​​next”这一行似乎有问题。 temp2 是子结构的变量。我不知道代码到底出了什么问题,这正是我需要帮助的地方。这是我的代码:

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

typedef struct Parent
{
char *value;
struct Child *next;
int numChildren;
}Parent;

typedef struct Child
{
char *value;
struct Child *next;
Parent *prev1;
struct Child *prev2;
}Child;

int isFirst(int parent[], int index)
{
int i;
for (i = 0; i < index; i++)
{
if (parent[i] == parent[index])
{
return 0;
}
}
return 1;
}

int addChild(Parent parents[], int index, char *num)
{
int i;
Child temp, temp2;
temp.value = num;
if (parents[index].numChildren == 0)
{
parents[index].next = &temp;
temp.prev1 = &parents[index];
temp.prev2 = NULL;
}
else
{
for (i = 0; i < parents[index].numChildren; i++)
{
if (i == 0)
{
temp2 = *parents[index].next;
}
else
{
temp2 = *temp2.next; // setting temp2 to null?
}
}
temp.prev1 = NULL;
temp.prev2 = &temp2;
temp2.next = &temp;
}
}

int main() {

FILE *fp, *fp2;
char read_file [256];
char current[256];
char space = ' ';
char *curr2, *curr3;
int i, index;
int j = 0;
int parent[] = {1, 1, 1, 3, 3, 4, 4, 1};
int child[] = {3, 4, 5, 7, 8, 10, 11, 100};
Parent parents[10];
Parent temp;
Child temp2, temp3;
int numParents = 0;
int done = 0;

fp = fopen ("/home/sam/parent.txt","w+");

if (fp == NULL) {
printf ("Error opening file: %s\n", strerror(errno));
exit(1);
}




for (i = 0; i < 8; i++)
{
fprintf(fp, "%d", parent[i]);
fprintf(fp, " ");
fprintf(fp, "%d", child[i]);
fprintf(fp, "\n");
}

for (i = 0; i < 8; i++)
{
fgets(current, 7, fp);
printf("%s\n", current);
curr2 = strtok(current, &space);
if (isFirst(parent, i) == 1)
{
parents[numParents].value = curr2;
parents[numParents].numChildren = 0;
numParents++;
}
printf("%s\n", curr2);
curr3 = strtok(NULL, &space);
while (done == 0)
{
temp = parents[j];
index = j;
if (temp.value == curr2)
{
done = 1;
}
j++;
}
j = 0;
printf("\n");
addChild(parents, numParents-1, curr3);
parents[index].numChildren++;
printf("%s\n", curr3);
done = 0;
}

fp2 = fopen ("/home/sam/parent2.txt","w+");

if (fp2 == NULL) {
printf ("Error opening file: %s\n", strerror(errno));
exit(1);
}

Child temp4;

for (i = 0; i < 3; i++)
{
fprintf(fp2, "%s", parents[i].value);
temp4 = *parents[i].next;
for (j = 0; j < parents[i].numChildren; j++)
{
fprintf(fp2, "%s", temp4.value); // segmentation fault occuring on this line second time through inner loop
fprintf(fp2, " ");
if (j < parents[i].numChildren-1)
{
temp4 = *temp4.next;
}
}
fprintf(fp2, "\n");
}
rewind(fp2);
for (i = 0; i < 3; i++)
{
fgets(current, 100, fp2);
printf("%s\n", current);
}
fclose(fp);
fclose(fp2);
return 0;
}

最佳答案

我认为您的问题已经从这里开始,在 addChild 函数中

int addChild(Parent parents[], int index, char *num)
{
    int i;
    Child temp, temp2;
    temp.value = num;
    if (parents[index].numChildren == 0)
    {
        parents[index].next = &temp; // <---
        temp.prev1 = &parents[index];
        temp.prev2 = NULL;
    }

您将局部变量的地址分配给 Parent 结构的成员。当函数返回时,this 将指向一个已经消失的对象(悬挂指针)。

以后对该指针的任何使用都会导致各种麻烦。

关于c - 段错误 - 文件写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166006/

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