C + + WriteFile and ReadFile use __c++

Source: Internet
Author: User
Tags readfile

Reading and writing files is a job that every Windows software developer needs to do. It can be seen that this work is very important, after all, all kinds of data need to be preserved in order to make a variety of analysis, or through the network to send to others. Like everyone with BT download movies, in that BT software, you need to constantly receive data from the network, and then save the data to the appropriate location in the file, you can generate the same file as the publisher, so that it can play out. For example, I am playing "journey" in the game, just open the game, it constantly download the updated files from the server down, and then save to the hard disk. The WriteFile function is used to write data to a file, and the ReadFile function is to read data from a file. But these two functions can not only read the file of the disk, but also can receive and send the network data, as well as read and write serial, USB, and other devices such as port data. In the read and write file, first of all, open the file, and then determine whether the open success. When writing a file, pay attention to the problem of whether the disk space is full or not. When reading a file, often need to read files in different locations, such as to read a 4G video file, it is impossible to read it completely in memory, so you need to locate the file read.

The functions WriteFile and ReadFile are declared as follows:
Winbaseapi
BOOL
WinAPI
WriteFile (
__in HANDLE hfile,
__in_bcount (nnumberofbytestowrite) lpcvoid lpbuffer,
__in DWORD Nnumberofbytestowrite,
__out_opt Lpdword Lpnumberofbyteswritten,
__inout_opt lpoverlapped 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 lpoverlapped
);

hfile is a file handle.
Lpbuffer is a read-write data buffer.
Nnumberofbytestowrite is how much data to write.
Lpnumberofbyteswritten is how much data has been written.
nNumberOfBytesToRead is how much data to read.
nNumberOfBytesToRead is how much data has been read.
Lpoverlapped is the structure of asynchronous read and write.


The 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"),//Create the name of the file.
#007 generic_write| Generic_read,//write and read files.
#008 0,//do not share read and write.
#009 NULL,//default security attribute.
#010 Create_always,//If file exists, also created.
#011 File_attribute_normal,//general documents.
#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 write successfully/r/n"));
#030}
#031
#032//Writes data from the write file buffer to the disk first.
#033 flushfilebuffers (hfile);
#034
#035//
Read the data from the file #036//.
#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 Read file successfully/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}

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/VirtualDesk/archive/2009/07/25/4379965.aspx

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.