gpt4 book ai didi

C:将文本文件复制到链接列表中会导致异常:抛出异常:读取访问冲突。当前为 0x2C2DE900

转载 作者:行者123 更新时间:2023-11-30 20:08:31 25 4
gpt4 key购买 nike

编辑:针对出现的新问题更新了问题:|

现在,解决了最后一个问题后,我又遇到了另一个问题。复制头部后的帧。函数“addFrame”抛出标题中所示的异常:(

这是我复制的文本文件

T1
C:\opencv\GIF Project\Photos\Tank_1.jpg
1000
T2
C:\opencv\GIF Project\Photos\Tank_2.jpg
1000
T3
C:\opencv\GIF Project\Photos\Tank_3.jpg
1000

复制功能:

/*
loadGIF: This function loads the GIF from the text file
Input: char filePath[STRING_SIZE = 50]: The path to the text file
frameNode* head: The head of the linked list which we load the GIF into
Output: None
*/
void loadGIF(char filePath[], frameNode** head)
{
frameNode* temp = NULL;
FILE* file = fopen(filePath, "r");
char name[STRING_MAX] = { 0 }, path[STRING_MAX] = { 0 }, line[STRING_MAX] = { 0 };
int counter = 0, sleep = 0, i = 0;
while (fgets(line, sizeof(line), file))
{
if (counter == THREE)
{
counter = 0;
}
if (counter == 0)
{
strcpy(name, line);
name[strcspn(name, "\n")] = 0;
}
if (counter == ONE)
{
strcpy(path, line);
path[strcspn(path, "\n")] = 0;
}
if (counter == TWO)
{
sleep = line;
atoi(sleep);
if (i == 0)
{
*head = createFrame(name, path, sleep);
}
else
{
temp = createFrame(name, path, sleep);
addFrame(&head, temp);
}
i++;
}
//memset(name, 0, sizeof(name));
//memset(path, 0, sizeof(path));
counter++;
}
fclose(file);
}

我在其中调用函数的主部分:

int main(void)
{
int i = 0, choise = -1, newGif = -1, sleep = 0, place = 0;
char path[STRING_MAX] = { 0 }, name[STRING_MAX] = { 0 };
frameNode* head = NULL, *temp = NULL;
newGif = loadingFunction();
if (newGif == 1)
{
printf("Enter The Path To The Text File Which Holds The GIF's Data: \n");
fgets(path, STRING_MAX, stdin);
path[strcspn(path, "\n")] = 0;
loadGIF(path, &head);
}
// The rest doesn't matter
}

这是addFrame函数:

/*
addFrame: This function addes a new frame to the GIF
Input: frameNode* head: The head of the linked list ( The first frame of the GIF )
newFrame*: The new frame to be added
Output: None
*/
void addFrame(frameNode* head, frameNode* newFrame)
{
frameNode* current = head;
while (current->next != NULL) // <-- Exception thrown here
{
current = current->next;
}
current->next = newFrame;
newFrame->next = NULL;
}

我认为问题出在链接列表的创建中,但我无法指出它。

如果你们需要的话,我会添加任何其他内容。我知道以前曾有人问过类似的问题,我检查了它们,这似乎对我的事业没有帮助。

非常感谢!

最佳答案

这是因为counter不断增加,永远无法满足您的条件。

Read my comments inline.

if (counter == 0)
{
strcpy(name, line);
name[strcspn(name, "\n")] = 0;
counter++; //increment the counter **kiran**
}
if (counter == 1)
{
strcpy(path, line);
path[strcspn(path, "\n")] = 0;
counter++; //increment the counter **kiran**
}
if (counter == 2)
{
atoi(line);
sleep = line;
if (i == 0)
{
*head = createFrame(name, path, sleep);
}
else
{
temp = createFrame(name, path, sleep);
addFrame(&head, temp);
}
counter = 0; //reset the counter
i++;
}
//memset(name, 0, sizeof(name));
//memset(path, 0, sizeof(path));
// counter++; //remove this line **kiran**
<小时/>

更新以处理第二个问题。

  addFrame(&head, temp);

应该是

addFrame(*head, temp);

到目前为止,您正在传递 frameNode *** 但它应该是 frameNode *

关于C:将文本文件复制到链接列表中会导致异常:抛出异常:读取访问冲突。当前为 0x2C2DE900,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56748158/

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