Poedu-windows Stage Class "PO School" Windows programming lesson004_003-2 file operations

Source: Internet
Author: User

    • different versions of the 001_ function
      • The Handle:createfile () function returns a handle to a kernel object
      • WINAPI: A calling convention that invokes a method.
      • _in_ and _in_opt_: There is no meaning in itself, a description macro, to indicate the nature of this parameter.
        • _in_ Description This parameter is an input parameter
        • _in_opt_ Description This parameter is the input pointer type parameter
        • _out_ Description This parameter is the" output type "parameter
          • output parameter Number to protect: Output type parameters, with operable space
      • VS2015, CreateFile () is a macro:
        Winbaseapi HANDLE WINAPI Createfilea (
        _in_ LPCSTR lpFileName,
        ......
        );
        ?
        Winbaseapi HANDLE WINAPI Createfilew (
        _in_ LPCWSTR lpFileName,
        ......
        );
        ?
        #ifdef UNICODE
        #define CreateFile Createfilew
        #else
        #define CreateFile Createfilea
        #endif//! Unicode
        • Windows platform programming, about the processing of characters, need to distinguish between two major camps: 1 wide character set and 2 narrow character set
        • "Wide" and "narrow" two different sets of bytes, will lead to different types, the initial CreateFile function support is narrow byte, and later found that the narrow byte in the multi-lingual environment, is not enough, the Windows designers, the CreateFile function into a macro, This macro allows CreateFile to have two versions of Createfilea () and Createfilew (). Createfilea () is compatible with narrow bytes, and Createfilew () promotes wide bytes.
        • The actual use of the process, causing a lot of inconvenience:
          • Example 1:
            wchar_t *filename = L "D:\\1.txt";
            CreateFile (filename,......);
          • Look at the sample code, where the programmer sets the filename with a wide byte, but when the external transform is a "narrow" byte set, the CreateFile macro can be converted to Createfilew, and wchar_t * filename cannot be automatically compatible with "narrow" bytes. So the "TCHAR" macro appears, and the "TEXT ()" macro appears on the right.
          • TEXT () This macro, the function is: when the environment is Unicode, the string is preceded by "L", otherwise, "L".
          • The above package of processing, can not be ported to Linux, need to be completely rewritten.
    • 002_createfile Parameter Explanation
      • See Chinese translation below
      • Transaction-based operations:
        • Divide an operation into "read" "Write" "Modify" "complete" 4 parts, Middle 123 any part fails, then the operation is invalid, "rollback" to the state before the operation.
        • For example, the program installation, can be made into a "transactional" operation, only when the overall completion, the transaction-type operation, the actual completion, otherwise "rollback" to the state before the installation.
        • Transactional operations are considered only when synchronizing, and transactional operations are not applicable when asynchronous.
      • lpFileName file name
        • In the ANSI version, the MAX_PATH macro replaces the 260;unicode version with no limit length.
      • dwDesiredAccess permissions: What permissions to open files
        • Can be used with multiple permissions overlay
        • If set to 0, the device does not have read and write permissions. For example, when we access the creation time of a file, we do not need to read and write the file, this setting is very suitable.
      • dwShareMode sharing mode
        • A series of patterns shared to others.
        • If it is set to 0, the other program takes up the file and cannot be opened again.
        • When access mode conflicts, there is GetLastError return error_sharing_violation
        • After the kernel object is opened, it needs to be closed. Otherwise, the permissions for this setting have been applied.
      • lpSecurityAttributes Security Descriptor
        • If NULL, it cannot be inherited by the quilt process
      • dwcreationdisposition What to do if the file exists or not
        • Create_always problem creating a new file
          • When the file is rewritten successfully, set Last-error code to Error_already_exists
          • The file does not exist, and the Last-error code is set to 0 when the creation succeeds.
        • Create_new a new file is created only if the file does not exist
          • If the file exists, execution fails, Last-error code is set to Error_file_exists
        • Open_always always open a file
          • File exists, execution succeeded, set to Error_already_exists
          • The file does not exist, the path name is valid and writable, a new file is created and Last-error code is set to 0.
        • Open_existing open a file only when it exists
          • File does not exist, it is set to Error_file_not_found
        • Truncate_existing testing whether a file exists
          • Opens a file only if it exists and intercepts its size to 0 bytes
          • File does not exist, function execution fails, Last-error code is Error_file_not_found
      • dwFlagsAndAttributes
    • 003_createfile complete
      • dwflagsandattributes The attribute is associated with the tag bit
        • file_flag_backup_semantics
          • Backup or restore
        • file_flag_no_buffering
        • file_flag_write_through
          • Write operation is not buffered by any intermediate buffer, write back directly to disk
      • hTemplateFile the same object as the kernel
        • is generally null
        • When not NULL, the previous parameter, dwflagsandattributes all the settings, will inherit this parameter, which is the newly given kernel object, which inherits all flags of this object.
      • return value
        • Return file handle
        • If it fails, return Last-error code to Invalid_handle_value
      • Note See MSDN History issues, troubleshooting issues, viewing when problems arise
        • After opening the handle, you want to close it.
    • CreateFile Chinese Translation:
      • function function
          • createfile function is used to create or open a file or I/O device. The most commonly used I/O devices are as follows:
            • file
            • file stream
            • folder
            • Physical disk
            • Logical disk drive
            • console program buffer
            • tape
            • communication resource
            • mail slots
            • pipeline
      • Function prototypes
      • parameter Resolution
        • lpFileName
          • 1 specifies the name of the file or device to open, create. You can use a slash (/) or a backslash (\) in your name
          • 2 in the ANSI version of this function, the name length is limited to MAX_PATH characters. In order to extend this limit to 32,767 wide characters, you need to call the Unicode version of this function and add a "\?\" prefix to the path name in addition. For more information, see Naming Files, Paths, and namespaces
          • 3 to obtain a name for the special device, see Defining an MS-DOS device name
          • 4 in order to create a file stream, you need to specify a filename, a colon plus the name of the stream file. For more information, see File Streams
        • dwdesiredaccess
          • Specifies what permissions to open files or devices, which can be summed up as: Read access, write access, read and write access, non-read non-write access
          • If the parameter value is 0, the program can ask for some metadata, such as file, directory, or device properties, without accessing the file or device. Additionally, the
        • dwsharemode
          • settings file or device sharing mode, including read, write, read/write, delete, full permissions or none of the above permissions (refer to the table below). This parameter does not affect access requests to properties and extended properties
        • lpsecurityattributes
          • The bInheritHandle member of the struct is used to set whether the returned handle can be inherited
        • dwCreationDisposition
          • Use to set what action to take on a file or device when the file exists or does not exist
          • For devices, this parameter is typically set to open_existing
          • For more information, see the remarks in this article
          • The value of this parameter must be one of the following values, and only one cannot be combined:
        • dwFlagsAndAttributes
            • file or device property values and tag bits, File_attribute_normal is the most common default value for files
            • Note: When CreateFile opens an existing file, It typically combines file attributes and file tag bits, and ignores attribute values defined in dwFlagsAndAttributes. For a detailed example see Creating and Opening Files
            • The following properties and tag bits may only apply to opening a file, not to a device that supports all other CreateFile functions. For more information, see the Remarks section of this document and Creating and Opening Files. For more information about file attributes, see the SetFileAttributes function. You can also see the full description of the values and descriptions of all the file properties in Attribute Constants
        • hTemplateFile
          • Legal handle to a template file that has Generic_read access
          • This template file provides properties and extended properties for files that are about to be created
          • The parameter value can be NULL
          • If you open a file that already exists, the CreateFile function ignores this parameter
          • If you open a new encrypted file, this function inherits the discretionary access control list from its parent directory. For more information, see File encryption

Poedu-windows Stage Class "PO School" Windows programming lesson004_003-2 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.