Reputation: 29697
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?
Upvotes: 1
Views: 1757
Reputation: 9404
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));
do not forget call fflush(file), before call FlusFileBuffers.
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;
}
Upvotes: 2
Reputation: 490623
flush
only flushes the internal buffers kept by the standard library code.
To flush the OS's buffers, you'd need/want to call FlushFileBuffers
(after calling f.flush()
).
Upvotes: 0