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

标签 c visual-c++ stream filehandle fflush

我正在尝试运行一些代码,但 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/

相关文章:

c - 关于将 -1 转换为 size_t 导致循环卡住

c - && 运算符在两个字符串之间时有何作用?

c - 海湾合作委员会 : Return statement from a void function in C

visual-studio - 为什么TortoiseHg认为Resource.h是二进制文件?

c++ - 无法在 C# 中加载 64 位 dll 错误

java - 向现有 2D 数组 + I/O 流添加新名称和分数?

c - C 中 printf 中表达式的求值

.net - 内存泄漏问题

ffmpeg - 使用#EXT-X-DISCONTINUITY-SEQUENCE [ffmpeg] 下载 ts 流

iphone - 如何从 iPhone 向 Windows 发送消息?