Functions such as createfile

Source: Internet
Author: User
Tags readfile

During the past few days, I have watched Windows API training every day. The blogger has drawn a channel. I will fill in the specific content.

 

Requirement: Operating Files, serial ports, parallel ports, USB, etc.

Principle layer: it is actually very simple. There are three steps: open, operate, and close.

Practice Layer 1: Open createfile, use writefile and readfile for read and write operations, and disable closehandle.

Practice Layer 2: Learn how to use each API.

 

Learning the API doesn't mean learning how to use parameters first, then looking at the returned values.

 

1. createfile

<Function> createfile: open or create a file, serial port, parallel port, USB, and so on. You can also open the path.

<Return value> handle is returned successfully. If dwcreationdisposition is set to create_always or open_always, getlasterror returns error_already_exists. Invalid_handle_value is returned for failure.

<Parameter description>

HandleCreatefile (

LpctstrLpfilename,File Path. ANSI: Maximum max_path characters; UNICODE: Maximum 32767, and
Prepend "\\? \ "To the path.

DWORDDwdesiredaccess,Access method, such as generic_read

DWORDDww.mode,Read/write sharing. When the value is 0, read/write is not shared, and the object cannot be opened again. Do not conflict with the access method.

Lpsecurity_attributesLpsecurityattributes,When null, the handle cannot be inherited.

DWORDDwcreationdisposition,Create_always: always create a new one. Create_new: failed if it exists. Open_always: create a new one if it does not exist. Open_existing: failed if it does not exist.

DWORDDwflagsandattributes,File_attribute_normal is enough.

HandleHtemplatefilenull

);

For objects in this three-step method of opening, operating, and closing, various attributes are usually specified during opening, and these attributes will only be kept before closing.

Path creation: createdirectory and createdirectoryex

What are the attributes reserved by the file. Concerned attributes: Size and time.

What are the attributes that are available only after a file is opened:

The following attribute scopes: open to close the file. This parameter is specified when it is enabled and disappears when it is disabled.

File pointer attributes, shared attributes, access attributes, creation attributes, file identity and attributes, and security attributes. The first four are important. File pointer is a changeable value. When reading and writing data, you can set its value to change the read/write position.

2. writefile and readfile

2-1. writefile

<Function> write data to the position specified by the file pointer. Synchronous and asynchronous. Writefileex only supports asynchronous operations.

<Return value> If the return value is successful, the return value is not 0. If the return value is failed, the return value is 0.

<Parameter description>

BOOL WriteFile(
Handle Hfile,Generic_write access handle. Asynchronous write: The handle opened is identified by the file_flag_overlapped file, or the socket handle returned by the socket or accept.
Lpcvoid Lpbuffer,Write Data Buffer
DWORD Nnumberofbytestowrite,Size of bytes to be written
Lpdword Lpnumberofbyteswritten, [Out]The number of bytes written. If lpoverlapped is null, this parameter cannot be null. If lpoverlapped is not null, this parameter can be null. For overlapped write operations, you can use getoverlappedresult to obtain the written bytes. If the hfile is associated with the I/O completion port, use getqueuedcompletionstatus to obtain the size of the written bytes.
Lpoverlapped Lpoverlapped This attribute is required only when hfile is opened as a file_flag_overlapped file. The hfile opened with file_flag_overlapped must not be null. Complete the port.
);

2-2. readfile

<Function> read data from the specified position of the file pointer. Synchronous and asynchronous. Readfileex is only used for asynchronous operations.

<Return value> If the return value is successful, the return value is not 0. If the return value is unsuccessful, the return value is 0. If the returned value is not 0 but the read bytes are 0, the file pointer ends at the end of the file when the read operation occurs. Readfile is returned only when any of the following conditions occur:

After the MPs queue is written, you will be notified of the completion of the write operation.

The requested byte has been read.

Ø an error occurs.

<Parameter description>

BOOL ReadFile(
Handle Hfile,Generic_read. For Asynchronous read: The handle opened is identified by the file_flag_overlapped file, or the socket handle returned by the socket or accept.
Lpvoid Lpbuffer, [Out]Buffer for receiving data
DWORD Nnumberofbytestoread,Size of the byte to be read
Lpdword Lpnumberofbytesread, [Out]Read bytes. Overlapped is not mentioned.
Lpoverlapped LpoverlappedNo.
);

2-3. flushfilebuffers

<Function> write cached data to a file.

<Return value> success is not 0, and failure is 0. When the handle is console output, it fails because the console output is not cached.

<Parameter description>

BOOL FlushFileBuffers(
Handle HfileGeneric_write access handle.
);

For an I/O device that does not support caching, open it with the file_flag_no_buffering file flag when calling createfile.

2-4. setfilepointer/* typedefunsigned
Long DWORD ;*/

<Function> move the pointer to an opened file. When the file pointer is larger than long, it is easier to use setfilepointerex.

<Return value> ① if the operation succeeds and lpdistancetomovehigh is null, the return value is the low position of the file pointer (DWORD type. ② Lpdistancetomovehigh is not null, and the returned value is the low position of the file pointer, which is stored in lpdistancetomovehigh. ③ If an error occurs and lpdistancetomovehigh is null, the returned value is invalid_set_file_pointer. ④ If the lpdistancetomovehigh fails and is not null, the returned value is invalid_set_file_pointer. ⑤ If the new file pointer is negative, the function fails. getlasterror returns error_negative_seek.

<Parameter description>

DWORD SetFilePointer(
Handle Hfile,Generic_read or generic_write.
Long Ldistancetomove, [In, out]The low length of the file pointer to be moved, with a positive or negative number.
Plong Lpdistancetomovehigh,The high length of the file pointer from the header or tail. When 32 bits are enough, this value is set to null.
DWORD DwmovemethodStart position of the file pointer: file_begin, file_current, or file_end.
);

2-5. setendoffile

<Function> move the end position of a file to the position of the current file pointer. You can use this function to cut off or expand the file size. If the file is increased, the content between the old EOF position and the new end position is not defined. Set the physical end location of the file, set the logical end location, and use setfilevaliddata.

<Return value> success is not 0, and failure is 0.

<Parameter description>

BOOL SetEndOfFile(
Handle Hfile The file handle accessed in generic_write mode.
);

3. closehandle can close the file handle.

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.