如何在win32上刷新stdlib输出文件? [英] How do I flush a stdlib output file on win32?
问题描述
我有一个C ++程序,正在写入一个文件在Windows 7上。当我调用 f.flush()
NTFS文件不会变大。有没有办法强制文件被刷新?
I have a C++ program that is writing to a file on Windows 7. When I call f.flush()
the NTFS file does not get bigger. Is there any way to force the file to get flushed?
推荐答案
如何获取文件HANDLE fopen FILE结构?
代码如下:
You can look here:
How do I get the file HANDLE from the fopen FILE structure?
the code looks like this:
FlushFileBuffers((HANDLE) _fileno(_file));
不要在调用FlusFileBuffers之前调用fflush(文件)。
do not forget call fflush(file), before call FlusFileBuffers.
对于std :: fstream和gnu stl,我在我的linux盒子上检查过,在家没有窗口,
但应该与mingw一起工作,可能需要一些修改:
For std::fstream and gnu stl, I checked it on my linux box, have no windows at home, but should work with mingw, may be need some modifications:
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <fstream>
#include <ext/stdio_filebuf.h>
int main(int argc, char *argv[])
{
assert(argc == 2);
std::ifstream f(argv[1]);
assert(!!f);
/*
//cin, cout etc
__gnu_cxx::stdio_filebuf<char> *stdio_buf = dynamic_cast<__gnu_cxx::stdio_filebuf<char> *>(f.rdbuf());
if (stdio_buf != 0) {
printf("your fd is %d\n", stdio_buf->fd());
return EXIT_SUCCESS;
}
*/
std::basic_filebuf<char> *file_buf = dynamic_cast<std::basic_filebuf<char> *>(f.rdbuf());
if (file_buf != 0) {
struct to_get_protected_member : public std::basic_filebuf<char> {
int fd() { return _M_file.fd(); }
};
printf("your fd is %d\n", static_cast<to_get_protected_member *>(file_buf)->fd());
}
printf("what is going on?\n");
return EXIT_FAILURE;
}
这篇关于如何在win32上刷新stdlib输出文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!