Windows API one-day training (54) writefile and readfile Functions

Source: Internet
Author: User
Read/write files are Windows What software developers need to do. It can be seen that this work is very important. After all, all kinds of data need to be saved for various analyses or transmitted to others through the network. For example BT The downloaded movie, where BT In the software, you need to constantly receive data from the network, and then save the data to a proper location in the file to generate the same file as the publisher, in this way, you can play the video. For example, when I started the game in the game "Journey", it continuously downloaded the updated files from the server and saved them to the hard disk. Writefile A function is used to write data to a file, Readfile A function reads data from a file. However, these two functions can not only read files written to the disk, but also receive and send network data, as well as read/write serial ports, USB And interport. In a read/write file, you must first open the file and then determine whether the file is successfully opened. When writing a file, you must also check whether the disk space is full or not. When reading files, you often need to read files in different locations, for example, to read 4G It is impossible to completely read the video file to the memory. Therefore, you need to locate and read the file. Function Writefile And Readfile The statement is as follows: Winbaseapi Bool Winapi Writefile ( _ In handle hfile, _ In_bcount (nnumberofbytestowrite) lpcvoid lpbuffer, _ In DWORD nnumberofbytestowrite, _ Out_opt lpdword lpnumberofbyteswritten, _ Inout_opt lpoverlapped ); Winbaseapi Bool Winapi Readfile ( _ In handle hfile, _ Out_bcount_part (nnumberofbytestoread, * lpnumberofbytesread) lpvoid lpbuffer, _ In DWORD nnumberofbytestoread, _ Out_opt lpdword lpnumberofbytesread, _ Inout_opt lpoverlapped ); Hfile Is the file handle. Lpbuffer Is the read/write data buffer. Nnumberofbytestowrite The amount of data to be written. Lpnumberofbyteswritten Is the amount of data written. Nnumberofbytestoread The amount of data to be read. Nnumberofbytestoread Is the amount of data that has been read. Lpoverlapped Is the structure of asynchronous read/write. 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.