Cfiledialog and cfile & createfile usage and sample-

Source: Internet
Author: User

 

Createfile usage and example

Function prototype:

Handle createfile (

Lptstr lpfilename, // pointer to the file name
DWORD dwdesiredaccess, // access mode (write/read)
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
);
Parameter List
Lpfilename string name of the file to be opened
Dwdesiredaccess long: generic_read indicates that read access to the device is allowed; generic_write indicates that write access to the device is allowed (can be used in combination); if it is zero, indicates that only information related to one device can be obtained.
Dww.mode long, zero indicates that the file is not shared; file_1__read and/or file_1__write indicates that shared access to the file is allowed.
Lpsecurityattributes security_attributes, pointing to a security_attributes structure pointer, defines the file security features (if supported by the operating system)
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 marks the file as compressed, or marks it as the default compression mode of the file in the directory.
File_attribute_normal default attribute
File_attribute_hidden: hide a file or directory
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 allows overlapping operations on files
File_flag_no_buffering prohibits file caching. The file can only be written into the Sector blocks of the disk volume.
File_flag_random_access optimizes the File Buffer for Random Access
File_flag_sequential_scan optimizes the File Buffer for continuous access
File_flag_delete_on_close closes the last opened handle and deletes the file. Especially suitable 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 will copy the extended attributes from this file.

Return Value

If the execution 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 region or write it to another file to check whether it is read correctly.
  17. Free (pbuffer );
  18. }

 

2. Pass in the file address through parameters:

  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. }

 

1. Save the dialog box cfiledialog filedlg (false );
Filedlg. m_ofn.lpstrtitle = "my file storage dialog box ";
Filedlg. m_ofn.lpstrfilter = "text files (*. txt)/0 *. txt/0all files (*. *)/0 *. */0/0 ";
Filedlg. m_ofn.lpstrdefext = "TXT ";
Filedlg. m_ofn.lstructsize = 88; // The 98 Style dialog box when the value is 76. The 98 Style dialog box is used by default.
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: modecreate | cfile: modewrite); // these two classes are connected here.
File. Write ("http://www.sunxin.org", strlen ("http://www.sunxin.org "));
File. Close ();
} 2. Open the dialog box cfiledialog filedlg (true );
Filedlg. m_ofn.lpstrtitle = "my file opening dialog box ";
Filedlg. m_ofn.lpstrfilter = "text files (*. txt)/0 *. txt/0all files (*. *)/0 *. */0/0 ";
 
If (idok = filedlg. domodal ())
{
Cfile file (filedlg. getfilename (), cfile: moderead );
Char * pbuf;
DWORD dwfilelen;
Dwfilelen = file. getlength ();
Pbuf = new char [dwfilelen + 1];
Pbuf [dwfilelen] = 0;
File. Read (pbuf, dwfilelen );
File. Close ();
MessageBox (pbuf );
}

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.