Createfile, writefile, readfile

Source: Internet
Author: User
Note: createfile is different from fopen. When opening a file, it does not distinguish text or binary readfile or writefile. Both operations are performed on binary data. Handle winapi createfile (_ in lpctstr lpfilename, // file path _ in DWORD dwdesiredaccess, // access permission, generic_read | generic_write _ in DWORD dw1_mode, // sharing mode, null | file_1__read | file_1__write _ in 1_lpsecurityattributes, // Security Attribute _ in DWORD dwcreationdisposition, // create a configuration, create_always | create_n EW | open_always | open_existing _ in DWORD dwflagsandattributes, // extended attribute, null | file_attribute_hidden | file_attribute_readonly _ in handle htemplatefile // specify a file handle, the new file copies the extended attributes from the file. Null); bool winapi readfile (_ in handle hfile, // file handle _ out lpvoid lpbuffer, // buffer _ in DWORD nnumberofbytestoread for receiving data, // number of bytes to be read _ out lpdword lpnumberofbytesread, // actual number of bytes read _ in lpoverlapped // overlapped structure, which is generally set to null ); bool winapi writefile (_ in handle hfile, // file handle _ in lpcvoid lpbuffer, // data to be written _ in DWORD nnumberofbytestowrite, // number of bytes to write _ out lpdword lpnumberofb Yteswritten, // Number of actually written bytes _ in lpoverlapped // overlapped structure, generally set to null); DWORD winapi getfilesize (_ in handle hfile, // file handle _ out lpdword lpfilesizehigh // return value of high-order, which is generally set to null); Read File: void read (const wchar_t * strpath) {handle hfile = :: createfile (strpath, generic_read, 0, null, open_existing, null, null); If (hfile! = Invalid_handle_value) {DWORD nfilesize =: getfilesize (hfile, null); char * lpbuffer = new char [nfilesize]; DWORD nnumberofbytesread; bool Bret =: readfile (hfile, lpbuffer, nfilesize, & nnumberofbytesread, null); // todo... delete [] lpbuffer; closehandle (hfile) ;}} Write File: void write (const wchar_t * strpath, const wchar_t * strcontent, const int ncontentsize) {handle hfile = :: createfile (strpath, gener Ic_write, 0, null, create_always, null, null); If (hfile! = Invalid_handle_value) {DWORD nnumberofbyteswritten;: writefile (hfile, strcontent, ncontentsize, & encoding, null); // todo... 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.