文本文件分割~~(50分)

  • 主题发起人 主题发起人 shadowpj
  • 开始时间 开始时间
S

shadowpj

Unregistered / Unconfirmed
GUEST, unregistred user!
我有个几百K的文本文件。我要分为诺干个64K的。最后一个不满也占一个。比如有500K那么分为7个64K的和个52K的。谢谢了。
 
procedure TForm1.Save1Click(Sender: TObject);

var
NewFileName: string;
Msg: string;
NewFile: TFileStream;
OldFile: TFileStream;
begin
NewFileName := ExtractFilePath(Application.ExeName) + ExtractFileName(Edit1.Text);
Msg := Format('Copy %s to %s?', [Edit1.Text, NewFileName]);
if MessageDlg(Msg, mtCustom, mbOKCancel, 0) = mrOK then
begin
OldFile := TFileStream.Create(Edit1.Text, fmOpenRead or fmShareDenyWrite);
try
NewFile := TFileStream.Create(NewFileName, fmCreate or fmShareDenyRead);

try
NewFile.CopyFrom(OldFile, Size);//主要是这里的Size=64*1024*1024
finally
FreeAndNil(NewFile);
end;
finally
FreeAndNil(OldFile);
end;
end;

end;
 
大哥根据小弟的要求写代码,不是我偷懒啊。是真的不懂哦。你的我不怎么明白size
 
var
NewFileName: string;
count,i:integer;
NewFile: TFileStream;
OldFile: TFileStream;
const FGSIZE=64*1024*1024;
begin
begin
OldFile := TFileStream.Create(原来的文件, fmOpenRead);
OldFile .Position=0;
if OldFile mod FGSIZE=0
count:=trunc(oldfile/fgsize);
else
count:=trunc(oldfile/fgsize)+1;
for i:=0 to count-1 do
begin
NewFile := TFileStream.Create(新文件, fmCreate or fmShareDenyRead);
try
NewFile.CopyFrom(OldFile, FGSize);
finally
FreeAndNil(NewFile);
end;
end;
finally
FreeAndNil(OldFile);
end;
end;
应该是这样,具体代码自己写吧,就是生成新文件名一点点了
 
谢谢,测试中。完了散分。。。
 
恩谢谢你了。我散分你。。呵呵。搞定了。
var
NewFileName: string;
count,i:integer;
NewFile: TFileStream;
OldFile: TFileStream;
const FGSIZE=64*1024;
begin
OldFile := TFileStream.Create('D:/Documents and Settings/Administrator/桌面/UserInfo.txt', fmOpenRead);
OldFile.Position:=0;
if OldFile.Size mod FGSIZE=0 then
count:=trunc(oldfile.size/fgsize)
else
count:=trunc(oldfile.size/fgsize)+1;
for i:=0 to count-1 do
begin
NewFile := TFileStream.Create('D:/Documents and Settings/Administrator/桌面/'+inttostr(i)+'.txt', fmCreate or fmShareDenyRead);
try
begin
if i= count-1 then
NewFile.CopyFrom(OldFile,oldfile.size-trunc(oldfile.size/fgsize)*fgsize)
else
NewFile.CopyFrom(OldFile, FGSize);
end;
finally
FreeAndNil(NewFile);
end;
end;
FreeAndNil(OldFile);
 
多人接受答案了。
 
后退
顶部