//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 显示“打开源文件”对话框
OpenDialog1->Title="源 文件";
if( OpenDialog1->Execute() )
Edit1->Text = OpenDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
// 显示另保为对话框
SaveDialog1->Title ="另 存为";
if( SaveDialog1->Execute() )
Edit2->Text = SaveDialog1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
int iFromFileHandle, iToFileHandle; // 声明文件对象
int iFileLength; // 文件长度
int iBytesRead,iBytesWritten; // 实际读、写文件的字节数
char *pszBuffer; // 缓冲区
try
{
// 打开源文件
iFromFileHandle = FileOpen(Edit1->Text, fmOpenRead);
// 显示文件大小信息
iFileLength = FileSeek(iFromFileHandle, 0, 2);
Edit3->Text = IntToStr(iFileLength)+"字节" ;
pszBuffer = new char[2049];
FileSeek(iFromFileHandle, 0, 0) ;//将文件指针移到文件开始处
// 打开目标文件
iToFileHandle = FileCreate(Edit2->Text);
// 拷贝文件
do
{
iBytesRead = FileRead(iFromFileHandle, pszBuffer, 2048);
iBytesWritten = FileWrite(iToFileHandle, pszBuffer, iBytesRead);
} while( iBytesRead==2048 );
ShowMessage ("拷贝文件完毕。") ;
delete[] pszBuffer;
FileClose(iFromFileHandle); // 关闭源始文件
FileClose(iToFileHandle); // 关闭目标文件
}
catch (...)
{
Application->MessageBox("运行过程中出错。","错误", IDOK);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Close(); // 关闭程序
}
//---------------------------------------------------------------------------