gpt4 book ai didi

c - 如何修复 EncodeTextApp.exe 中的错误 : Exception thrown at 0x00007FFC284DDDFF (ucrtbased. dll):0xC0000005

转载 作者:行者123 更新时间:2023-11-30 20:04:15 32 4
gpt4 key购买 nike

我昨天写了这段代码。它编译/构建良好,没有错误,并打开 Windows 命令提示符,要求我输入原始文本。我输入我希望程序编码的原始文本,然后按 Enter 键。这会导致以下错误:

Exception thrown at 0x00007FFC284DDDFF (ucrtbased.dll) in EncodeTextApp.exe: 0xC0000005: Access violation writing location 0x00007FF7CAEC9C23.

If there is a handler for this exception, the program may be safely continued.

当我在程序运行时在命令提示符中输入要编码的消息时,会发生上述异常。然后它就崩溃了。

下面的代码是我的应用程序中的代码。我不知道我是否在这里做错了什么。

#include <stdlib.h>
#include <conio.h>
#include <iostream>

#define MAX_LEN 80
#define encodingshift 17

char* encode(char* str) {
int i = 0;
while (str[i] != '\0') {
str[i] = str[i] - encodingshift;
/*str[i] = (char)((int)str[i] + encodingshift) % 26;*/
}
return (str);
}

void main() {
char *str = "";

printf("Please enter the message to encode: ");
fgets(str, MAX_LEN, stdin);
char* original = str;

str = encode(str);
printf("\nOriginal Message: %s", original);
printf("\nEncoding Shift: %d", encodingshift);
printf("\nEncoded message: %s", str);
getchar();
}

我已经完成了所有操作,从从 Visual Studio 卸载 C++ 项目并重新安装它们,到再次安装整个 Visual Studio,但错误并没有消失。因此,我想知道我必须做什么。这是阻止我的程序运行的唯一因素。

最佳答案

char *str = "";

printf("Please enter the message to encode: ");
fgets(str, MAX_LEN, stdin);

是错误的,str 是一个字符串文字(通常放置在只读段上,因此不可修改),并且您需要空间来使用 fgets 存储字符串,请更改至:

char str[MAX_LEN];

str = encode(str); /* str now is an array, you can not assign to */

encode(str);

请注意,您没有在 while 循环(无限循环)中递增 i

此外,您正在混合 C 和 C++,不要这样做,如果您想要 C,iostream 应该是 stdio.h 并使用有效的签名 main:int main(void)而不是void main()

关于c - 如何修复 EncodeTextApp.exe 中的错误 : Exception thrown at 0x00007FFC284DDDFF (ucrtbased. dll):0xC0000005,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466467/

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