How can I write something to a file in C++ without using system cache and drive cache? I just want to write exactly on the hdd regardless all the system cache settings.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unless you are writing a disk device driver, you can’t guarantee that there won’t be any cache or processing done with your write.
The C runtime library exposes
fflush(FILE *)to do this. Windows hasFlushFileBuffersas well as a flag you can pass toCreateFile(FILE_FLAG_NO_BUFFERING) (which itself adds restrictions on what you can do).An alternative to trying to bypass write caching is to make your data structures resilient to partial failure. For files, one popular technique is to write out the header of the file after the rest of the file is written. Assuming Murphy and not Machiavellian behavior, that should be enough.
Or use the OS provided file replacement or transaction functions (See ReplaceFile and Transactional NTFS for Windows).