The CreateFile, ReadFile, and WriteFile of file operations

Source: Internet
Author: User
Tags readfile

The following functions are typically used to communicate with a peripheral device through the win system:

-------------------------------

1. CreateFile

The function is to create or open a file or I/O device , commonly used in the form of I/O file, file stream, directory, physical Disk, volume, terminal flow and so on. If the execution succeeds, the file handle is returned . Invalid_handle_value indicates an error and the GetLastError is set.
The declaration definition of a function:

HANDLE WINAPI CreateFile (  _in_      lpctstr lpfilename,                _in_      DWORD dwdesiredaccess,  _in_      DWORD dwShareMode,  _in_opt_  lpsecurity_attributes lpsecurityattributes,  _in_      DWORD dwCreationDisposition,  _in_      DWORD dwflagsandattributes,  _in_opt_  HANDLE htemplatefile);

Parameter list:

2. ReadFile

The data is read out to a file from the location pointed to by the file pointer (device file, communication) , and synchronous and asynchronous operations are supported, and if the file is opened in a way that does not indicate file_flag_overlapped, when the program call succeeds, It saves the number of bytes actually read from the file to the address space indicated in Lpnumberofbytesread. File_flag_overlapped allows overlapping operations on files.
function declaration Definition:

BOOL WINAPI ReadFile (  __in          HANDLE hfile,                   //  file handle  __out         lpvoid lpbuffer,                //  buffer  __in DWORD nnumberofbytestoread for receiving data          ,     // Number of bytes to read  __out         Lpdword lpnumberofbytesread,    //  number of bytes actually read  __in          lpoverlapped lpoverlapped       //);

Code examples:,

1BOOL Read (Char*FilePath)2 {3 HANDLE PFile;4 DWORD fileSize;5     Char*buffer,*Tmpbuf;6 DWORD Dwbytesread,dwbytestoread,tmplen;7 8PFile =CreateFile (Filepath,generic_read,9 File_share_read,Ten NULL, One         open_existing,   //open a file that already exists A File_attribute_normal, - NULL); -     //Create a device file, return the location of the file the     if(PFile = =Invalid_handle_value) -     { -printf"Open File error!\n"); - CloseHandle (pFile); +         returnFALSE; -     } +      AFileSize = GetFileSize (pfile,null);//get the file size at  -Buffer = (Char*) malloc (fileSize); - ZeroMemory (buffer,filesize); -Dwbytestoread =fileSize; -dwBytesRead =0; -Tmpbuf =buffer; in  -      Do{//cycle through the files to ensure that the full file is read to  +ReadFile (pfile,tmpbuf,dwbytestoread,&dwbytesread,null); -  the         if(dwBytesRead = =0) *              Break; $ Panax NotoginsengDwbytestoread-=dwBytesRead; -Tmpbuf + =dwBytesRead; the  +} while(Dwbytestoread >0); A  the         //TODO handles read data in buffer +  - Free (buffer); $ CloseHandle (pFile); $  -     returnTRUE; -}
3. WriteFile

Writes data to a file (device file, communication). This function is much more flexible than the fwrite function. This function can also be applied to the processing of communication devices, pipelines, sockets, and postal slots. When returned, TRUE (not 0) indicates success, otherwise zero is returned. The GetLastError is set.
function declaration Definition:

BOOL WINAPI WriteFile (  __in          HANDLE hfile,                   //  file handle  __in          lpcvoid lpbuffer,               //  data to be written  __in          DWORD nnumberofbytestowrite,    //  Number of bytes to write  __out         //  number of bytes actually written  __in          lpoverlapped lpoverlapped       // OVERLAPPED structure, generally set to NULL);

Example code:

1BOOL Write (Char*buffer, DWORD Contentlen)2 {3 HANDLE PFile;4     Char*Tmpbuf;5 DWORD Dwbyteswrite,dwbytestowrite;6 7PFile =CreateFile (Filepath,generic_write,8         0,9 NULL,Ten        Create_always, //Always create Files One File_attribute_normal, A NULL); -  -     if(PFile = =Invalid_handle_value) the     { -printf"Create file error!\n"); - CloseHandle (pFile); -         returnFALSE; +     } -  +Dwbytestowrite =Contentlen; ADwbyteswrite =0; at  -Tmpbuf =buffer; -  -      Do{//Loop through the file to ensure that the complete file is written -  -WriteFile (pfile,tmpbuf,dwbytestowrite,&dwbyteswrite,null); in  -Dwbytestowrite-=Dwbyteswrite; toTmpbuf + =Dwbyteswrite; +  -} while(Dwbytestowrite >0); the  * CloseHandle (pFile); $ Panax Notoginseng     returnTRUE; -}

Endl

The CreateFile, ReadFile, and WriteFile of file operations

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.