Win32 API file read and write operations

Source: Internet
Author: User
Tags readfile

1. Creation and opening of files

HANDLE CreateFile (

LPCTSTR lpFileName,

DWORD dwDesiredAccess,

DWORD dwShareMode,

Lpsecurity_attributes lpSecurityAttributes,

DWORD dwCreationDisposition,

DWORD dwFlagsAndAttributes,

HANDLE hTemplateFile

);

lpFileName: Specifies the name of the object to create or open;

dwDesiredAccess: Specifies how objects are accessed, including:

0--Specify the object can be accessed by device query;

Generic_read--Specify object readable access, can read data from the file, and can move the pointer in the file;

Generic_write--Specify object writable access, can write data to the file, and can move the pointer in the file;

dwShareMode: Specify how to share, including:

File_share_delete

File_share_read

File_share_write

lpSecurityAttributes: A pointer to a security_attributes structure that determines whether the returned handle can be inherited by the quilt process;

dwCreationDisposition: Specifies how files are created, including:

Create_new--Creates a new file, and if the file already exists, the function call fails;

Create_always--Create a new file, and if the file already exists, empty the file's existing properties

Open_existing--Open the file and the function call fails if the file does not exist

Open_always-If the file exists, open the file, or if the file does not exist, create a new file;

Truncate_existing--Open the file, the file is opened to be intercepted so that its size is 0 bytes, call the function must use Generic_write access to open the file, if the file does not exist, the function call fails;

dwFlagsAndAttributes: Set file properties and flags, including:

File_attribute_archive--The file is an archive file

File_attribute_hidden--The file is a hidden file

File_attribute_normal--The file has no other property settings

File_attribute_offline--File data has been physically moved to an offline storage device and cannot be used immediately

File_attribute_readonly--The file is read-only

File_attribute_system--The file is an operating system file

File_attribute_temporary--The file is temporarily stored for use

File_flag_write_through--instructs the system to write data directly to disk without being cached

File_flag_overlapped--instructs the system to initialize the file object so that those operations that take a long time to complete return the ERROR_IO_PENDING flag

File_flag_no_buffering--instructs the system to open the file without a system buffer

File_flag_random_access--Indicates that the file is a random access method

File_flag_sequential_scan--Indicates that the file is in sequential access mode

File_flag_delete_on_close-Indicates that the operating system will delete the file immediately after all handles to the file have been closed

File_flag_backup_semantics-Indicates that the file is opened or created for backup or storage operations

File_flag_posix_semantics--Indicates that the file will be accessed according to POSIX rules

File_flag_open_reparse_point--Specify this flag to prohibit the re-profiling behavior of NTFS reparse points

File_flag_open_no_recall-Indicates that the data for the file has been requested, but it continues to be stored in remote Storage and should not be returned to the local storage

hTemplateFile: If an existing file is opened, this parameter will be ignored; To make this argument valid must be satisfied: To create a new file, the file handle passed to the parameter must be opened using the Generic_read method;

2. Write the file

BOOL WriteFile (

HANDLE hfile,

Lpcvoid lpbuffer,

DWORD Nnumberofbytestowrite,

Lpdword Lpnumberofbyteswritten,

lpoverlapped lpoverlapped

);

hfile: Specifies a handle to the file to write data to

Lpbuffer: Pointer to the buffer that contains the data that will be written to the file

Nnumberofbytestowrite: Indicates the number of bytes to write to the file

Lpnumberofbyteswritten: The number of bytes used to receive the actual write to the file

lpoverlapped: A pointer to the overlapped struct, this parameter to work, you must add the file_flag_overlapped tag when CreateFile open the File Settings file property, telling the system to access the file asynchronously. By default, it is accessed in the form of synchronous IO

3, the file read

BOOL ReadFile (

HANDLE hfile,

LPVOID lpbuffer,

DWORD nNumberOfBytesToRead,

Lpdword Lpnumberofbytesread,

lpoverlapped lpoverlapped

);

hfile: Specifies a handle to the file whose data is to be read

Lpbuffer: A pointer to a buffer that will receive the data read from the file

nNumberOfBytesToRead: Specifies the number of bytes read from the file

Lpnumberofbytesread: The number of bytes used to receive the actual read

lpoverlapped: A pointer to the overlapped struct, this parameter to work, you must add the file_flag_overlapped tag when CreateFile open the File Settings file property, telling the system to access the file asynchronously. By default, it is accessed in the form of synchronous IO

Cases:

Define a handle variable handle hfile;//Create a file hfile = CreateFile ("1.txt", Generic_write, 0, NULL, Create_new, File_attribute_normal, NULL)///receive the actual number of bytes written by DWORD dwwrites;//Write Data WriteFile (hfile, "Hello world!", strlen ("Hello world!"), &dwwrites, NULL) ;//close file handle CloseHandle (hfile);
HANDLE hfile;//Open File hfile = CreateFile ("1.txt", generic_read, 0, NULL, open_existing, file_attribute_normal, NULL);// Receive the data actually read to the Char ch[100];//receive the actual number of bytes read to the DWORD dwreads;//read Data ReadFile (hfile, ch, +, &dwreads, NULL);//Set string end character ch[ Dwreads] = 0;//Closes the handle of the open File Object CloseHandle (hfile);//Displays the data read to the MessageBox (CH);

Win32 API file read and write 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.