Windows API one-day training (55) flushfilebuffers and setfilepointer Functions

Source: Internet
Author: User
In PC In the hardware architecture, the fastest memory is CPU Register in the system, then to the second-level cache, and then to the System Ram Memory, and finally to the hard disk. This architecture determines how the operating system operates on files, or is optimized.Algorithm. For example, when the operating system receives the data written to a file, it first saves the data Ram And then write it to the hard disk at the right time or when the data volume is suitable. But sometimes we want to save the data to the hard disk instead Ram In this case, you need to use the Function Flushfilebuffers To Ram To the hard disk. The structure of a file is an ordered queue with a head and tail. After reading and writing a file, the file pointer in the file will be moved. Sometimes you want to move to a specific location to read data. For example, read BMP File, which consists of a file header and a data block, you need to read the file header first, and then read the image display data according to the data block starting position indicated in the file header, then you need to use Setfilepointer Function. Function Flushfilebuffers And Setfilepointer The statement is as follows: Winbaseapi Bool Winapi Flushfilebuffers ( _ In handle hfile ); Winbaseapi DWORD Winapi Setfilepointer ( _ In handle hfile, _ In long ldistancetomove, _ In_opt plong lpdistancetomovehigh, _ In DWORD dwmovemethod ); Hfile Is the file handle. Ldistancetomove Is the length of the file pointer from the header or tail. Lpdistancetomovehigh Is the high length of the file pointer from the header or tail. Dwmovemethod Is relative to the file header, end, or current location. An example of calling a function is as follows: #001 // Create, write, and read files. #002 // Cai junsheng 2007/10/21 QQ: 9073204 Shenzhen #003 void createfiledemo (void) #004 { #005 // #006 handle hfile =: createfile (_ T ("createfiledemo.txt "),// The name of the created file. #007 generic_write | generic_read ,// Write and read files. #008 0 ,// Read/write is not shared. #009 null ,// Default Security attribute. #010 create_always ,// If the file exists, it is also created. #011 file_attribute_normal ,// Common file. #012 null );// The template file is empty. #013 #014 if (hfile = invalid_handle_value) #015 { #016 // #017 outputdebugstring (_ T ("createfile fail! \ R \ n ")); #018} #019 #020 // Write data to a file. #021 const int bufsize = 4096; #022 char chbuffer [bufsize]; #023 memcpy (chbuffer, "test", 4 ); #024 DWORD dwwritensize = 0; #025 bool Bret =: writefile (hfile, chbuffer, 4, & dwwritensize, null ); #026 if (BRET) #027 { #028 // #029 outputdebugstring (_ T ("writefile File written successfully \ R \ n ")); #030} #031 #032 // First, write the data in the file buffer to the disk. #033 flushfilebuffers (hfile ); #034 #035 // #036 // Read data from a file. #037 long ldistance = 0; #038 DWORD dwptr = setfilepointer (hfile, ldistance, null, file_begin ); #039 if (dwptr = invalid_set_file_pointer) #040 { #041 // Get error code. #042 DWORD dwerror = getlasterror (); #043 // Processing error. #044} #045 #046 DWORD dwreadsize = 0; #047 Bret =: readfile (hfile, chbuffer, 4, & dwreadsize, null ); #048 if (BRET) #049 { #050 // #051 outputdebugstring (_ T ("readfile File Read successful \ R \ n ")); #052} #053 else #054 { #055 // Get error code. #056 DWORD dwerror = getlasterror (); #057 // Processing error. #058 tchar cherrorbuf [1024]; #059 wsprintf (cherrorbuf, _ T ("getlasterror () = % d \ r \ n"), dwerror ); #060 outputdebugstring (cherrorbuf ); #061} #062 #063}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.