gpt4 book ai didi

c - 读取文件: Run-Time Check Failure #2 - Stack around the variable 'delta_c' was corrupted时运行时错误

转载 作者:行者123 更新时间:2023-11-30 17:55:33 24 4
gpt4 key购买 nike

使用 C 读取文件时出现奇怪的运行错误,错误消息是“运行时检查失败 #2 - 变量‘delta_c’周围的堆栈已损坏。”我使用的是 Visual Studio 2010

这是我运行错误的代码的简化版本(即使在这个简化版本上我也遇到运行错误)

#include "stdafx.h"

typedef struct {
unsigned short dx;
unsigned short dy;
unsigned char dr;
unsigned short ext;
unsigned char delta_c;
} Delta;

typedef struct {
unsigned char region;
unsigned char delta_num;
Delta delta[8];
} MNTDATSTRUCT;
int ReadAr(const char* mntfile, MNTDATSTRUCT *p)
{
FILE *fp = fopen(mntfile, "r");

fscanf(fp, "%u",&p->delta_num);
for (int i=0; i<p->delta_num; ++i) {
unsigned short dx, dy, ext;
unsigned char delta_c;
fscanf(fp,"%u%u%u%u", &dx, &dy, &ext, &delta_c);
p->delta[i].dx = dx;
p->delta[i].dy = dy;
p->delta[i].ext = ext;
p->delta[i].delta_c = delta_c;

}
fclose(fp);
return 0;
}

int main()
{
MNTDATSTRUCT p;
ReadAr("F:\\projects\\test_c++\\Debug\\tt.bpm",&p);
return 0;
}

我计划读取的数据文件(即 tt.bpm)如下所示:

1 
1 2 10 13

请注意,由于某些原因,struct Delta 的某些字段未显示在数据文件中(unsigned char dr)。也适用于结构 MNTDATSTRUCT 中的无符号字符区域

当我尝试运行代码时,Visual studio 将在第 33 行停止(就在 ReadAr 返回 0 之后),并弹出运行时错误消息:运行时检查失败 #2 - 变量“delta_c”周围的堆栈已损坏。”,尽管在监 window 口中,MNTDATSTRUCT *p 已被属性填充。

我对这种情况很好奇。谁能给我一些建议吗?

感谢您的帮助。

最佳答案

您需要为短整型添加 h 修饰符并使用 hh 字符。

fscanf(fp, "%hhu",&p->delta_num);

fscanf(fp,"%hu%hu%hu%hhu", &dx, &dy, &ext, &delta_c);

关于c - 读取文件: Run-Time Check Failure #2 - Stack around the variable 'delta_c' was corrupted时运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14211853/

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