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 ValueIf 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:
- VoidPlaycewav ()
- {
- Char* Pbuffer;
- DWORDRsize;
- IntFilesize = 0;
- IntI;
- HandleHopenfile = (Handle) Createfile (L "E: // A. Text", generic_read, file_1__read, null, open_existing, null, null );
- If(Hopenfile = invalid_handle_value)
- {
- Hopenfile = NULL;
- Messageboxa (null, "can not open the file", "playwav", mb_ OK );
- }
- Filesize = getfilesize (hopenfile, null );
- Pbuffer = (Char*) Malloc (filesize );
- Readfile (hopenfile, pbuffer, filesize, & rsize, null );
- // You can display pbuffer in a region or write it to another file to check whether it is read correctly.
- Free (pbuffer );
- }
2. Pass in the file address through parameters:
- VoidPlaywav (Tchar* Path)
- {
- Char* Pbuffer;
- DWORDRsize;
- IntFilesize = 0;
- IntI;
- TcharSzpath [100];
- Memset (szpath, 0,Sizeof(Szpath ));
- _ Tcscpy (szpath, PATH );
- HandleHopenfile = (Handle) Createfile (szpath, generic_read, file_cmd_read, null, open_existing, null, null );
- If(Hopenfile = invalid_handle_value)
- {
- Hopenfile = NULL;
- Messageboxa (null, "can not open the file", "playwav", mb_ OK );
- }
- Filesize = getfilesize (hopenfile, null );
- Pbuffer = (Char*) Malloc (filesize );
- Readfile (hopenfile, pbuffer, filesize, & rsize, null );
- Free (pbuffer );
- }
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 ); } |