C ++ copy files (using windowsapi)

Source: Internet
Author: User

Bool tform1: copyfile2 (){
Ansistring srcpath = extractfilepath (Application-> exename) + "windows.pdf ";
Ansistring destpath = extractfilepath (Application-> exename) + "dest.pdf ";

Handle hsrcfile, hdestfile;
Hsrcfile = createfile (srcpath. c_str (), // open myfile. txt
Generic_read, // open for reading
File_pai_read, // share for reading
Null, // No security
Open_existing, // existing file only
File_attribute_normal, // normal file
Null); // No ATTR. Template
If (hsrcfile = invalid_handle_value)
Return false;

Hdestfile = createfile (destpath. c_str (), // create myfile. txt
Generic_write, // open for writing
0, // do not share
Null, // No security
Create_always, // overwrite existing
File_attribute_normal, // normal file
Null );
If (hdestfile = invalid_handle_value)
Return false;
Char * buffer = new char [1024];

Ulong lpnumberofbytesread, lpnumberofbyteswritten;
DWORD filesize, offset = 0;
Filesize = getfilesize (hsrcfile, null );
If (filesize = invalid_file_size)
Return false;
While (offset <filesize ){
Bool bflag = readfile (hsrcfile, buffer, 1024, & lpnumberofbytesread, null );
If (! Bflag ){
Return false;
}
Writefile (hdestfile, buffer, 1024, & lpnumberofbyteswritten, null );
Offset ++ = 1024;
}
Closehandle (hsrcfile );
Closehandle (hdestfile );
Delete [] buffer;
Return true;
}

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.