gpt4 book ai didi

c - fflush 在 Visual C++ 2010 上失败

转载 作者:太空宇宙 更新时间:2023-11-04 04:06:12 24 4
gpt4 key购买 nike

我正在尝试运行一些代码,但 fflush() 出现错误:

 Invalid file descriptor. File possibly closed by a different thread

这是代码的相关部分:

    fhandle = fopen("dbfile.bin", "rbc");
/* There is a valid dbfile.bin file - get file size */
_fstat(_fileno(fhandle), &file_stat);
printf("dbfile.bin size = %d\n", file_stat.st_size);

g_tpd_list = (tpd_list*)calloc(1, file_stat.st_size);

if (!g_tpd_list)
{
rc = MEMORY_ERROR;
}
else
{
fread(g_tpd_list, file_stat.st_size, 1, fhandle);
fflush(fhandle);
fclose(fhandle);
}

最佳答案

奇怪的是,这种行为似乎是由您将 'c' 模式传递到您的 fopen 调用中引起的。帮助是关于标志的:

Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called

所以我不确定为什么它会导致它按现在的方式运行。但是,如果您删除它,fflush 调用会起作用。可能是此标志撤消 fflush 清除读取缓冲区并使其始终尝试清除写入缓冲区的能力。

关于c - fflush 在 Visual C++ 2010 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5630616/

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