gpt4 book ai didi

c - 在 C 中释放指针时显示垃圾值

转载 作者:太空宇宙 更新时间:2023-11-04 07:34:04 25 4
gpt4 key购买 nike

<分区>

当我尝试释放 dataToWrite 时,我不确定为什么在 if 循环中抛出垃圾值。它在 else 循环中工作正常。谁能帮我解决这个问题?

FILE *readFilePointer = NULL;
Message_buf outputMsgBuffer;
char fileData[PIPE_BUF];
char *dataToWrite=NULL;

readFilePointer = fopen(fileToReadFrom, "r");

if (readFilePointer == NULL)
{
outputMsgBuffer.messageType=1;
dataToWrite=(char *)malloc(strlen(strcat(fileToReadFrom," does not exists!!"))+1);
memset(dataToWrite,0,strlen(dataToWrite));
dataToWrite=strcat(fileToReadFrom," does not exists!!");
strcat(outputMsgBuffer.messageText,dataToWrite);
memset(dataToWrite,0,strlen(dataToWrite)+1);
free(dataToWrite);
}
else
{
//outputMsgBuffer.messageText[0] = '\0';
while (fgets(fileData, sizeof(fileData), readFilePointer) != NULL)
{
dataToWrite=(char *)malloc(sizeof(fileData));
memset(dataToWrite,0,strlen(dataToWrite));
strcpy(dataToWrite,fileData);
strcat(outputMsgBuffer.messageText,dataToWrite);
free(dataToWrite);
}

fclose(readFilePointer);
}
outputMsgBuffer.messageType=1;
outputMsgBuffer.messageLength=strlen(outputMsgBuffer.messageText);
mesg_send(fd,&outputMsgBuffer);
}

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