gpt4 book ai didi

c - 从以前保存的文件加载时缓冲区溢出

转载 作者:行者123 更新时间:2023-11-30 14:26:39 25 4
gpt4 key购买 nike

我做了一个带有加载/保存选项的文本冒险。在我创建的游戏文件夹中,它运行良好。即使我制作了游戏的发行版本,它也会按其应有的方式运行。

但我必须把这个项目交给我的教授。在我的电脑上,保存和加载工作正常,但在我的笔记本电脑或项目文件夹的副本上,我收到错误消息“缓冲区溢出已发生”。那么我的错是什么?

我要加载/保存的代码:

bool save()
{
char sFileText[1024];
char sFileName[80];
char sFileDirectory[80];

GetCurrentDirectory(512, sFileDirectory);

sprintf(sFileName, "%s\\save_game.ini", sFileDirectory);

sprintf(sFileText, "[settings]\n\rsp_loop=%i\n\rroom_iSelect=%i\n\riSelect=%i\n\rraum=%i\n\rtor402=%i\n\rwegiSelect=%i\n\rwegweiser=%i\n\rlight=%i\n\ropen_door=%i\n\rmesser=%i\n\rmesserwurf=%i\n\rkamera=%i\n\rstein=%i\n\ralive=%i\n\rpfeil=%i\n\rluke=%i\n\rkuehlschrank=%i\n\rpulver_schmand=%i\n\rteddy=%i\n\rseil=%i\n\rgiftklinge=%i\n\r",
sp_loop,
room_iSelect,
iSelect,
raum,
tor402,
wegiSelect,
wegweiser,
light,
open_door,
messer,
messerwurf,
kamera,
stein,
alive,
pfeil,
luke,
kuehlschrank,
pulver_schmand,
teddy,
seil,
giftklinge
);


int cur_char;
FILE *out_file;

out_file = fopen(sFileName, "w");
if (!out_file == NULL)
{
for (cur_char = 0; cur_char < strlen(sFileText); ++cur_char)
{
fputc(sFileText[cur_char], out_file);
}
}
else
{
return false;
}

fclose(out_file);
return true;
}

bool open()
{
char sFileText[1024];
char sFileName[12];
char sFileGameSettingsIni[80];

sprintf(sFileName, "save_game.ini");



GetCurrentDirectory(512, sFileGameSettingsIni);

sprintf(sFileGameSettingsIni, "%s\\%s", sFileGameSettingsIni, sFileName);

sp_loop = GetPrivateProfileInt("settings", "sp_loop", 512, sFileGameSettingsIni);

raum = GetPrivateProfileInt("settings", "raum", 512, sFileGameSettingsIni);
tor402 = GetPrivateProfileInt("settings", "tor402",512, sFileGameSettingsIni);
wegiSelect = GetPrivateProfileInt("settings", "wegiSelect", 512, sFileGameSettingsIni);
wegweiser = GetPrivateProfileInt("settings", "wegweiser", 512, sFileGameSettingsIni);
light = GetPrivateProfileInt("settings", "light", 512, sFileGameSettingsIni);
open_door = GetPrivateProfileInt("settings", "open_door",512, sFileGameSettingsIni);
messer = GetPrivateProfileInt("settings", "messer", 512, sFileGameSettingsIni);
messerwurf = GetPrivateProfileInt("settings", "messerwurf", 512, sFileGameSettingsIni);
kamera = GetPrivateProfileInt("settings", "kamera", 512, sFileGameSettingsIni);
stein = GetPrivateProfileInt("settings", "stein", 512,sFileGameSettingsIni);
alive = GetPrivateProfileInt("settings", "alive", 512,sFileGameSettingsIni);
pfeil = GetPrivateProfileInt("settings", "pfeil", 512, sFileGameSettingsIni);
luke = GetPrivateProfileInt("settings", "luke", 512, sFileGameSettingsIni);
kuehlschrank = GetPrivateProfileInt("settings", "kuehlschrank", 512, sFileGameSettingsIni);
pulver_schmand = GetPrivateProfileInt("settings", "pulver_schmand", 512, sFileGameSettingsIni);
teddy = GetPrivateProfileInt("settings", "teddy", 512, sFileGameSettingsIni);
seil = GetPrivateProfileInt("settings", "seil", 512, sFileGameSettingsIni);
giftklinge = GetPrivateProfileInt("settings", "giftklinge",512, sFileGameSettingsIni);

return true;
}

最佳答案

您将sFileName定义为sFileName[12],然后尝试向其中写入14个字节(13个字节和一个NULL)。您传递给 GetCurrentDirectory() 的缓冲区似乎也不够大。当您仅将缓冲区定义为 80 个字符宽时,您声明缓冲区的长度为 512 字节。

GetCurrentDirectory() 的返回值将告知您缓冲区需要多大。

关于c - 从以前保存的文件加载时缓冲区溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8827513/

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