Windows API Practice (FlushFileBuffers) and SetFilePointer functions

Source: Internet
Author: User
Tags readfile

in thePCin the hardware architecture, the fastest memory isCPUthe registers inside, then to the level two cache, then to the systemRAMmemory, and finally to the hard drive. Because of this architecture, it determines how the operating system operates on the file, or the algorithm that is optimized. For example, when the operating system receives the data of the written file, it will save the data toRAMand then write to the hard drive at the right time or the right amount of data. But sometimes we want the data to be saved to the hard drive, not saved inRAMyou need to use the functionflushfilebuffersto getRAMSave the data in the drive. The structure of the file is an orderly queue, symbolizing, when the file is read and written, it moves the file pointer in the file. Sometimes you want to move to a specific location to read the data. For example, reading aBMPFile , it has a file header and data block composition, it is necessary to read the file header, and then according to the file head indicates that the data block start position to read the image display data, you need to useSetFilePointerfunction. functionflushfilebuffersand theSetFilePointerdeclarations such as the following:WinbaseapiBOOLWINAPIFlushFileBuffers (__in HANDLE hfile    );    WinbaseapiDWORDWINAPISetFilePointer (__in HANDLE hfile,__in LONG Ldistancetomove,__in_opt Plong lpDistanceToMoveHigh,__in DWORD Dwmovemethod    ); hfileis the file handle. Ldistancetomoveis the length of the file pointer from the head or tail. lpDistanceToMoveHighis the length of the file pointer from the head or tail to a high length. Dwmovemethodis relative to the file header, the end of the file, or the current location. Examples of calling functions are as follows:#001//Create, write, and read files. #002//Cai Junsheng2007/10/21 qq:9073204Shenzhen#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,//do not share read and write. #009 NULL,//default security properties. #010 create_always,//assume that the file exists and is also created. #011 file_attribute_normal,//General documentation.        #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 the 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 ("WriteFileWrite file Success/r/n "));#030}#031 #032// the data written to the file buffer is first forced to disk. #033 flushfilebuffers (hfile);#034#035//#036//reads data from the file. #037 LONG ldistance = 0; #038 DWORD dwptr = SetFilePointer (hfile, Ldistance, NULL, file_begin);#039 if (dwptr = = Invalid_set_file_pointer)#040 {#041//Gets the 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 ("ReadFileRead File Success/r/n "));#052}#053 Else#054 {#055//Gets the 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}

Windows API Practice (FlushFileBuffers) and SetFilePointer functions

Related Article

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.