關於文件大小的問題?(10分)

  • 主题发起人 主题发起人 amli
  • 开始时间 开始时间
A

amli

Unregistered / Unconfirmed
GUEST, unregistred user!
能夠用WriteFile把文件增大,能不能縮小呢.<br>例如ABC.DAT原大小為1024B,想把大小更為12B,但不能重新創建,是不是要改FAT,有哪位高手知道.
 
用流的方式来写,你要多少写都行
 
怎麼搞,給你10分.
 
不能重新創建文件.
 
var<br>&nbsp; Stream1: TMemoryStream;<br>begin<br>&nbsp; Stream1:=TMemoryStream.Create;<br>&nbsp; OpenDialog1.Execute;<br>&nbsp; Stream1.LoadFromFile(OpenDialog1.FileName);<br>&nbsp; stream1.Position:=0;<br>&nbsp; stream1.Size:=10; &nbsp; // 只写10个字节<br>&nbsp; Stream1.SaveToFile('test.dat');<br>&nbsp; stream1.Free;<br>end;<br>
 
你沒有明的我意思.<br>Stream1.SaveToFile('abc.dat');//是不是要重創.
 
Stream1.SaveToFile('abc.dat'); // 不保存能行吗?<br>要不可以通过改变文件的大小的方法,但一样涉及到写的问题
 
寫文件可以.
 
procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; F: File;<br>&nbsp; Buf: array [0..0] of Char;<br>begin<br>&nbsp; AssignFile(F, 'test.dat');<br>&nbsp; ReWrite(F, 1);<br>&nbsp; try<br>&nbsp; &nbsp; BlockWrite(F, Buf, 1);<br>&nbsp; &nbsp; Seek(F, 11); //改变文件大小为11+1=12字节。<br>&nbsp; &nbsp; BlockWrite(F, Buf, 1);<br>&nbsp; finally<br>&nbsp; &nbsp; CloseFile(F);<br>&nbsp; end;<br>end;
 
兩種方法都差不多,能不能在'test.dat'文件打開的情操作.
 
// 从SaveToFile过程原型看<br>// 上例并不存在重创文件过程<br>procedure TCustomMemoryStream.SaveToFile(const FileName: string);<br>var<br>&nbsp; Stream: TStream;<br>begin<br>&nbsp; Stream := TFileStream.Create(FileName, fmCreate);<br>&nbsp; try<br>&nbsp; &nbsp; SaveToStream(Stream);<br>&nbsp; finally<br>&nbsp; &nbsp; Stream.Free;<br>&nbsp; end;<br>end;
 
//有呀:<br>constructor TFileStream.Create(const FileName: string; Mode: Word; Rights: Cardinal);<br>begin<br>&nbsp; if Mode = fmCreate then<br>&nbsp; begin<br>&nbsp; &nbsp; inherited Create(FileCreate(FileName, Rights));<br>&nbsp; &nbsp; if FHandle &lt; 0 then<br>&nbsp; &nbsp; &nbsp; raise EFCreateError.CreateResFmt(@SFCreateError, [FileName]);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; inherited Create(FileOpen(FileName, Mode));<br>&nbsp; &nbsp; if FHandle &lt; 0 then<br>&nbsp; &nbsp; &nbsp; raise EFOpenError.CreateResFmt(@SFOpenError, [FileName]);<br>&nbsp; end;<br>end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
922
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部