【发布时间】:2015-01-18 16:38:58
【问题描述】:
我在从文件加载数据时遇到问题。 这段代码用一些随机数据填充高分(仅用于调试)
int i = 0;
for (i = 0; i < MAX_HIGH_SCORE; i++)
{
high_score_name[i] = al_ustr_new("123456789012345678901");
high_score[i] = 1000*i;
}
之后我将所有内容保存到文件中。
ALLEGRO_FILE* high_score_file = al_fopen("hs.txt","wb");
for (i = 0; i < MAX_HIGH_SCORE; i++)
{
int buffer = al_ustr_size(high_score_name[i]);
al_fwrite32le(high_score_file, buffer);
al_fwrite(high_score_file, al_cstr(high_score_name[i]), buffer);
al_fwrite(high_score_file, &high_score[i], sizeof(high_score[i]));
}
al_fclose(high_score_file);
它工作得很好。它有 4 个字节为大小保留,然后是 21 个字符的 high_score_name[],然后是 high_score[] int,它也占用 4 个字节。
当我尝试加载它时问题就开始了。我尝试了很多方法,但它不会编译,因为行
char* buffer= al_malloc(size);
给出错误:
“void *”类型的值不能用于初始化“char *”类型的实体
for (i = 0; i < MAX_HIGH_SCORE; i++)
{
int size = al_fread32le(high_score_file);
char* buffer = al_malloc(size);
al_fread(high_score_file, buffer, size);
high_score_name[i] = al_ustr_new_from_buffer(buffer, size);
int *buffer2 = &high_score[i];
al_fread(high_score_file, buffer2, sizeof(high_score[i]));
al_free(buffer);
}
al_fclose(high_score_file);
我坚持了两天,我真的很感激任何帮助。
【问题讨论】:
-
尝试在调用
al_malloc( )之前添加演员表,即char *buffer = (char *) al_malloc(size);。 -
它帮助解决了错误,但现在我得到 0xC0000005:初始化大小时访问冲突读取位置 0xFEEEFEF6。
-
0xFEEEFEEE : Used by Microsoft's HeapFree() to mark freed heap memorystackoverflow.com/a/127404/487892 -
失败时 al_fread32le( ) 返回 -1。在 al_malloc( ) 调用之前需要一个 if 语句来确保 size > 0。
-
我在使用
al_fread32le();后添加了if(size>0)语句,但是当程序执行int size = al_fread32le(high_score_file);时出现访问冲突@ 大小值是-858993460并且在文件中它的15 00 00 00(即21十进制)