One-day training of Windows APIs (56) SetEndOfFile and GetFileSizeEx Functions

Source: Internet
Author: User
Tags readfile
One day, I was developing the btsoftware, which had the function of allocating the file size and then writing data to the corresponding location. The advantage is that the disk space can be occupied first, so that subsequent Downloads can proceed smoothly. To implement this function, you need to create an empty file, set the file pointer to the corresponding size, and then call the SetEndOfFile function to set the end position of the file, in this way, the file has a corresponding size. In the development of the btsoftware, It is also found that the file size in the directory needs to be recorded in detail during directory processing. This requires the GetFileSizeEx function to get the file size. Because the video files in BT are relatively large and may be several GB, you must use the GetFileSizeEx function for processing. In this way, you can obtain large files without making any errors. The FlushFileBuffers and SetFilePointer functions are declared as follows ); HFileIs the file handle. LpFileSizeReturns the size of the object. Example of calling a function: #001 // create, write, and read a file. #002 // Cai junsheng 2007/10/23 QQ: 9073204 Shenzhen #003 void CreateFileDemo (void) #004 {# 005 // #006 HANDLE hFile = :: createFile (_ T ("CreateFileDemo.txt"), // name of the file to be created. #007 GENERIC_WRITE | GENERIC_READ, // write and read files. #008 0, // do not share read/write. #009 NULL, // default security attribute. #010 CREATE_ALWAYS, // create a file if the file exists. #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 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 ("WriteFile file written successfully \ r \ n ")); #030} #031 #032 // write the data in the file buffer to the disk first. #033 FlushFileBuffers (hFile); #034 #035 // #036 // read 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 // obtain the error code. #042 DWORD dwError = GetLastError (); #043 // handle an 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 success \ r \ n ")); #052 }# 053 else #054 {#055 // obtain the error code. #056 DWORD dwError = GetLastError (); #057 // handle an error. #058 TCHAR chErrorBuf [1024]; #059 wsprintf (chErrorBuf, _ T ("GetLastError () = % d \ r \ n"), dwError ); #060 OutputDebugString (chErrorBuf); #061} #062 #063 // #064 // #065 // move the file pointer to a new position. #066 lDistance = 3; #067 dwPtr = SetFilePointer (hFile, lDistance, NULL, FILE_BEGIN); #068 #069 // Set the new end position of the file. #070: SetEndOfFile (hFile );#071 #072 // obtain the file size. #073 LARGE_INTEGER liFileSize; #074: GetFileSizeEx (hFile, & liFileSize );#075 #076 TCHAR chTemp [128]; #077 wsprintf (chTemp, _ T ("GetFileSizeEx () = % d \ r \ n"), liFileSize ); #078 OutputDebugString (chTemp); #079 #080 #081 // close the file. #082 if (hFile! = INVALID_HANDLE_VALUE) #083 {#084 // #085 CloseHandle (hFile); #086} #087 #088}

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.