Windows via C/C ++ Study Notes-"file devices" for device I/O"

Source: Internet
Author: User
Tags readfile

I didn't plan to write this article, but everyone knows the importance of the file. In device I/O, a device is called a file device. This is an abstract concept. You just need to understand it as a file.

File device, which can be opened through the createfile function to obtain a file object handle.

 

There are two important attributes in the file:

1. file size: the maximum size of a 32-bit file is 4 GB, and the size of a 64-bit file can reach 16 EB.

2. file read/write pointer: This pointer indicates the read/write location. The size range can be larger than the file size.

 

First, we will discuss the file size.

To get the file size, you can use the getfilesizeex function:

Bool getfilesizeex (
Handle hfile, // file object handle
Plarge_integer plifilesize); // large_integer Union pointer, return size

 

This function accepts a pointer combined with large_integer to return the file size. This structure can represent a 64-bit value:

Typedef union _ large_integer {
Struct {
DWORD lowpart; // low 32-bit value
Long highpart; // high 32-bit value

};
Longlong quadpart; // 64-bit value
} Large_integer, * plarge_integer;

 

From this definition, we can see that the Union can use quadpart to represent a 64-bit value, or the difference can be divided into two 32-bit values. This combination has an unsigned number version called ulager_integer union, and the three members are all saved unsigned numbers.

 

Another function can get the file size:

DWORD getcompressedfilesize (
Pctstr pszfilename, // file path name
Pdword pdwfilesizehigh); // if the file size is greater than 4 GB, the 32-bit high value is returned by this parameter.

 

This function accepts the path name of a file and returns the low 32-bit value of the file size. The high 32-bit value is returned by the pdwfilesizehigh parameter. Unlike getfilesizeex, this function returns the physical size of a file, while getfilesizeex returns the logical size of the file.

For example, if a file is kb and compressed to 85kb, if getfilesizeex is used, 100kb is returned and getcompressedfilesize is used, 85kb is returned.

Unlike getfilesizeex, this function accepts a string and specifies the file path. This allows you to directly query the size of a file, instead of opening it to obtain its handle.

You can use this function as follows:

Ularge_integer ulfilesize; // similar to large_integer combination, save the unsigned number
Ulfilesize. lowpart = getcompressedfilesize (text ("somefile. dat "),
& Ulfilesize. highpart); // obtain the size of the somefile. dat file in the current directory.

In this way, the 64-bit file size is stored in ulfilesize. quadpart.

 

After discussing the file size, we will discuss the file read/write pointers.

The createfile function creates or opens a file kernel object, which manages a "file read/write Pointer ". This pointer specifies a 64-bit offset. Initially, the pointer is set to 0, that is, when you read or write data, it starts from the beginning of the file, that is, from the point where the offset is 0. Each time you read or write n bytes of data, the system updates the read/write pointer to add n Bytes to the offset. For example, the following code reads the first 100 bytes of data from a file:

Byte pbfirst [50], pbsecond [50];
DWORD dwnumbytes;
Handle hfile = createfile (text ("myfile. dat"),...); // the pointer is initialized to 0.
Readfile (hfile, pbfirst, 50, & dwnumbytes, null); // read 0th ~ 49 bytes
Readfile (hfile, pbsecond, 50, & dwnumbytes, null); // read 50th ~ 99 bytes

 

Note that a file object handle corresponds to a read/write pointer. If a file is opened multiple times, there are multiple file objects. Each file object manages a read/write pointer, these pointers do not affect each other. For example, the following code:

Byte Pb [10];
DWORD dwnumbytes;
Handle hfile1 = createfile (text ("myfile. dat"),...); // the pointer is initialized to 0.
Handle hfile2 = createfile (text ("myfile. dat"),...); // the pointer is initialized to 0.
Readfile (hfile1, Pb, 10, & dwnumbytes, null); // read 0th ~ 9 bytes
Readfile (hfile2, Pb, 10, & dwnumbytes, null); // read 0th ~ 9 bytes

 

In the above Code, hfile1 and hfile2 are the handles of two different file kernel objects in the same file. These two kernel objects manage two different file pointers, so they change one of the read/write pointers, will not affect the other.

The following code illustrates the problem:

Byte Pb [10];
DWORD dwnumbytes;
Handle hfile1 = createfile (text ("myfile. dat"),...); // The read/write pointer is initialized to 0.
Handle hfile2; // another file handle

// Copy the hfile1 handle value in the current process to hfile2 in the current process
Duplicatehandle (
Getcurrentprocess (), hfile1,
Getcurrentprocess (), & hfile2,
0, false, duplicate_same_access );
Readfile (hfile1, Pb, 10, & dwnumbytes, null); // read 0th ~ 9 bytes
Readfile (hfile2, Pb, 10, & dwnumbytes, null); // read 10th ~ 19 bytes

 

In the above Code, the duplicatehandle function is used to copy the handle, so that the two handles hfile1 and hfile2 share the same file kernel object, so the Read and Write pointers are also shared.

 

You can use the setfilepointerex function to locate the file read/write pointer:

Bool setfilepointerex (
Handle hfile, // file kernel object handle
Large_integer lidistancetomove, // 64-bit, move bytes
Plarge_integer plinewfilepointer, // returns the new file read/write pointer location
DWORD dwmovemethod); // mobile mode

Dwmovemethod in this function tells the system how to move. File_begin indicates moving from the file header; file_end indicates moving forward from the end of the file; file_current indicates moving from the current read/write pointer position. The moving amount is in the lidistacetomove parameter of 2nd.

 

Notes

  • It is legal to set the position of the file read/write pointer to a value beyond the file size range. This will not increase the file size unless the setendoffile function is called.
  • When the createfile function is used to open a file, the dwflagsandattributes parameter of this function includes file_flag_no_buffering. The file read/write pointer can only be set to the unit size of the hard disk sector.
  • If you do not have the getfilepointerex function to obtain the pointer position of the current file, you can call the setfilepointerex function to obtain its position. Set the second parameter to 0, as shown in the following code:
Large_integer licurrentposition = {0 };
Setfilepointerex (hfile, licurrentposition,
& Licurrentposition, file_current );

 

When a file is closed, the system sets an end position on the file to determine the file size. Of course, you can also set the end position of the file to change the file size. Use the setendoffile function:

Bool setendoffile (handle hfile );

 

This file sets the end mark of the file at the current file read/write pointer to cut off or expand the file size. For example, if you want to set the size of a file to 1024 bytes, you can use the following code:

Handle hfile = createfile (...);
Large_integer lidistancetomove;
Lidistancetomove. quadpart = 1024;
// Set the file pointer
Setfilepointerex (hfile, lidistancetomove, null, file_begin );
Setendoffile (hfile); // set the end mark at the file pointer
Closehandle (hfile );

 

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.