WriteFile and ReadFile functions __ functions

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:
Create, write, and read files.
Cai Junsheng 2007/10/21 qq:9073204 Shenzhen
void Createfiledemo (void)
{
//
HANDLE hfile =:: CreateFile (_t ("CreateFileDemo.txt"),//Create the name of the file.
generic_write| Generic_read,//write and read files.
0,//do not share read and write.
NULL,//default security attribute.
Create_always,//If file exists, also created.
File_attribute_normal,//general documents.
NULL); The template file is empty.

if (hfile = = INVALID_HANDLE_VALUE)
{
//
OutputDebugString (_t ("CreateFile fail!\r\n"));
}

Write data into a file.
const int bufsize = 4096;
Char Chbuffer[bufsize];
memcpy (Chbuffer, "Test", 4);
DWORD dwwritensize = 0;
BOOL BRet =:: WriteFile (Hfile,chbuffer,4,&dwwritensize,null);
if (BRet)
{
//
OutputDebugString (_t ("WriteFile write file \ r \ n)");
}

Writes the data from the write file buffer to the disk first.
FlushFileBuffers (hfile);

//
Read data from a file.
LONG ldistance = 0;
DWORD dwptr = SetFilePointer (hfile, Ldistance, NULL, File_begin);
if (dwptr = = Invalid_set_file_pointer)
{

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.