FileRead函数怎么不工作?(100分)

  • 主题发起人 主题发起人 Kriss
  • 开始时间 开始时间
K

Kriss

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了以下几行代码:<br>var srcFileHandle:integer;nFileSize:integer;<br>&nbsp; &nbsp; buf:PChar;nReadCount:integer;<br>begin<br>&nbsp; &nbsp; srcFileHandle:=FileOpen('e:/download/wmp7s.exe',fmOpenRead);<br>&nbsp; &nbsp; nFileSize:=FileSeek(srcFileHandle,0,2);<br>&nbsp; &nbsp; FileSeek(srcFileHandle,0,0); //回到文件开头<br>&nbsp; &nbsp; buf:=PChar(AllocMem(nFileSize));<br>&nbsp; &nbsp; nReadCount:=FileRead(srcFileHandle,buf,nFileSize);<br>经过我的调试,我发现FileOpen,FileSeek函数都工作正常,但是在FileRead函数中,它的<br>返回值总是-1,就是出错,为什么,我百思不得其解,求教各位了!
 
你的程序没有错,你为什么要使用EXE文件来试,你试个文本文件就可以,你可以用Delphi<br>的例子来试,读EXE结果还是-1
 
那就是说这个函数只能用来读TXT文件么?那要读其他格式的文件用什么方法呢?一定要ASSIGNFILE,<br>然后用READ,RESET等这些函数么?
 
不是有读二进制文件的函数呀,c++builder 有,delphi我不是很清楚
 
仔细看看delphi帮助中的例子吧:<br><br>procedure TForm1.Button1Click(Sender: TObject);<br><br>var<br>&nbsp; iFileHandle: Integer;<br>&nbsp; iFileLength: Integer;<br>&nbsp; iBytesRead: Integer;<br>&nbsp; Buffer: PChar;<br>&nbsp; i: Integer<br>begin<br>&nbsp; if OpenDialog1.Execute then<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);<br>&nbsp; &nbsp; &nbsp; iFileLength := FileSeek(iFileHandle,0,2);<br>&nbsp; &nbsp; &nbsp; FileSeek(iFileHandle,0,0);<br>&nbsp; &nbsp; &nbsp; Buffer := PChar(AllocMem(iFileLength + 1));<br>&nbsp; &nbsp; &nbsp; iBytesRead := FileRead(iFileHandle, Buffer, iFileLength);<br>&nbsp; &nbsp; &nbsp; FileClose(iFileHandle);<br><br>&nbsp; &nbsp; &nbsp; for i := 0 to iBytesRead-1 do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; StringGrid1.RowCount := StringGrid1.RowCount + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; StringGrid1.Cells[1,i+1] := Buffer;<br>&nbsp; &nbsp; &nbsp; &nbsp; StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffer));<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FreeMem(Buffer);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
多人接受答案了。
 
后退
顶部