Createfile usage and example

Source: Internet
Author: User

 

Function prototype:

Handle createfile (

Lptstr lpfilename, // pointer to the file name
DWORD dwdesiredaccess, // write/read mode)
DWORD dw1_mode, // share mode
Lpsecurity_attributes lpsecurityattributes, // pointer to the Security Attribute
DWORD dwcreationdisposition, // how to create
DWORD dwflagsandattributes, // file attributes
Handle htemplatefile // used to copy the file handle
);
Quota List
Lpfilename string name of the file to be opened
Dwdesiredaccess long is assumed to be generic_read, indicating that the device is allowed to read; if it is generic_write, it indicates that the device is allowed to write a reply question (which can be used in combination); if it is zero, indicates that you only agree to obtain information related to a device.
Dw1_mode long, zero indicates that the file is not shared; file_1__read and/or file_1__write indicates that the file is shared.
Lpsecurityattributes security_attributes, pointing to a security_attributes structure pointer, defines the file security features (assuming the operating system supports)
Dwcreationdisposition long, one of the following constants:
Create_new: Creates a file. If the file exists, an error occurs.
Create_always: Creates a file and changes the previous file.
The open_existing file must already exist. Requirements from devices
Open_always: Create an open_always file if it does not exist.
Truncate_existing indicates that the existing file is shortened to zero length.
Dwflagsandattributes long, one or more of the following Constants
File_attribute_archive mark archive attributes
File_attribute_compressed: Mark the file as compressed, or mark it as the default compression mode of the file in the folder.
File_attribute_normal default attribute
File_attribute_hidden: hide a file or folder
The file_attribute_readonly file is read-only.
The file_attribute_system file is a system file.
The file_flag_write_through operating system must not postpone file write operations.
File_flag_overlapped agrees to overlap files
File_flag_no_buffering prohibits file caching. The file can only be written into the sector block of the disk volume.
File_flag_random_access optimizes the File Buffer for random seek questions
File_flag_sequential_scan optimizes the File Buffer for consecutive seek questions
File_flag_delete_on_close closes the last opened handle and deletes the file. Special for temporary files
You can also combine the following constant tags in Windows NT:
Security_anonymous, security_identification, security_impersonation, security_delegation, security_context_tracking, security_inclutive_only
Htemplatefile long. If it is not zero, a file handle is specified. The new file copies the extended attribute returned values from this file. If the operation is successful, the file handle is returned.

Invalid_handle_value indicates an error and getlasterror is set. Even if the function succeeds, if the file exists and create_always or open_always are specified, getlasterror is set to error_already_exists.

(From Baidu encyclopedia)

 

Instance:

1. Specify the file address directly in the function:

  1. VoidPlaycewav ()
  2. {
  3. Char* Pbuffer;
  4. DWORDRsize;
  5. IntFilesize = 0;
  6. IntI;
  7. HandleHopenfile = (Handle) Createfile (L"E: // A. Text", Generic_read, file_1__read, null, open_existing, null, null );
  8. If(Hopenfile = invalid_handle_value)
  9. {
  10. Hopenfile = NULL;
  11. Messageboxa (null,"Can not open the file","Playwav", Mb_ OK );
  12. }
  13. Filesize = getfilesize (hopenfile, null );
  14. Pbuffer = (Char*) Malloc (filesize );
  15. Readfile (hopenfile, pbuffer, filesize, & rsize, null );
  16. // You can display pbuffer in a certain area or write a file to check whether the file is read correctly.
  17. Free (pbuffer );
  18. }

 

2. Data Transmission of the file address through hosts:

  1. VoidPlaywav (Tchar* Path)
  2. {
  3. Char* Pbuffer;
  4. DWORDRsize;
  5. IntFilesize = 0;
  6. IntI;
  7. TcharSzpath [100];
  8. Memset (szpath, 0,Sizeof(Szpath ));
  9. _ Tcscpy (szpath, PATH );
  10. HandleHopenfile = (Handle) Createfile (szpath, generic_read, file_cmd_read, null, open_existing, null, null );
  11. If(Hopenfile = invalid_handle_value)
  12. {
  13. Hopenfile = NULL;
  14. Messageboxa (null,"Can not open the file","Playwav", Mb_ OK );
  15. }
  16. Filesize = getfilesize (hopenfile, null );
  17. Pbuffer = (Char*) Malloc (filesize );
  18. Readfile (hopenfile, pbuffer, filesize, & rsize, null );
  19. Free (pbuffer );
  20. }

 

 

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.