麻烦大家看一下这段代码!(50分)

  • 主题发起人 microwave
  • 开始时间
M

microwave

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么我用这段代码获取一个mp3文件的最后128字节,什么也得不到?'p' 始终为空!why???<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FileHandle,filelength:integer;<br>&nbsp; P:pchar;<br>begin<br><br>&nbsp; &nbsp; &nbsp; &nbsp; P:=Allocmem(130);<br>&nbsp; &nbsp; &nbsp; &nbsp; filehandle:=fileopen('01.mp3',fmOpenWrite or fmShareDenyNone);<br>&nbsp; &nbsp; &nbsp; &nbsp; if filehandle &gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filelength:=fileseek(filehandle,0,2);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileseek(filehandle,filelength-128,0);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileread(filehandle,p,3);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fileclose(filehandle);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage(p);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; freemem(p);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showmessage('error:'+pchar(getlasterror));<br><br><br><br>end;
 
p:改为Pointer试试<br>检查filelength:=fileseek(filehandle,0,2);<br>&nbsp; &nbsp; fileseek(filehandle,filelength-128,0);的返回值<br><br>
 
你是要读取ID3信息吧?我有一个类,可以做到。<br>你的文件指针定位没有那么麻烦:直接用FileSeek(Fhandle,128,2)就可以了。<br>那个P必须改成p^就可以了。
 
To Kingron:<br>&nbsp; &nbsp;给我那个类可不可以?<br>&nbsp; &nbsp;
 
严黎斌<br>unit ID3Kernel;<br><br>interface<br><br>type<br>&nbsp; Tid3v1= record<br>&nbsp; &nbsp; Tag: array[0..2] of char; &nbsp; &nbsp; &nbsp;//00..02 , ='TAG'<br>&nbsp; &nbsp; Title:array[0..$1d] of char; &nbsp;//03..20<br>&nbsp; &nbsp; Artist:array[0..$1d] of char; &nbsp;//21..3e<br>&nbsp; &nbsp; Album:array[0..$1d] of char; &nbsp;//3f..5c<br>&nbsp; &nbsp; Year:array[0..3] of char; &nbsp; &nbsp; &nbsp;//5d..60<br>&nbsp; &nbsp; Comment:array[0..$1c] of char; //61..7d<br>&nbsp; &nbsp; Track:byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//7e<br>&nbsp; &nbsp; Genre:byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//7f<br>&nbsp; end;<br><br>function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>function DeleteID3v1(strFile:string):integer;<br><br>implementation<br><br>function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte;<br>&nbsp; i: integer;<br>begin<br>&nbsp; result:=1;<br>&nbsp; if strFile='' then exit;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=0;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit;<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $7f do Read(f1,bytAll);<br>&nbsp; if (bytAll[0]&lt;&gt;ord('T')) and (bytAll[1]&lt;&gt;ord('A'))<br>&nbsp; &nbsp; and (bytAll[2]&lt;&gt;ord('G')) then exit; // no 'TAG' found<br>&nbsp; Move(bytAll,pid3v1,$80);<br>&nbsp; CloseFile(f1);<br>&nbsp; result:=0;<br>end;<br><br>function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte;<br>&nbsp; i: integer;<br>begin<br>&nbsp; result:=1;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=2;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit;<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $2 do Read(f1,bytAll); // test if 'TAG' exists<br>&nbsp; if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))<br>&nbsp; &nbsp; &nbsp; &nbsp; and (bytAll[2]=ord('G'))<br>&nbsp; &nbsp; then Seek(f1,FileSize(f1)-$80)<br>&nbsp; &nbsp; else Seek(f1,FileSize(f1));<br>&nbsp; Move(pid3v1,bytAll,$80);<br>&nbsp; for i:=0 to $7f do Write(f1,bytAll);<br>&nbsp; CloseFile(f1);<br>&nbsp; result:=0;<br>end;<br><br>function DeleteID3v1(strFile:string):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte;<br>&nbsp; i: integer;<br>begin<br>&nbsp; Result:=1;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=2;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit;<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $2 do Read(f1,bytAll); // test if 'TAG' exists<br>&nbsp; if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))<br>&nbsp; &nbsp; &nbsp; &nbsp; and (bytAll[2]=ord('G'))<br>&nbsp; &nbsp; then begin<br>&nbsp; &nbsp; &nbsp; Seek(f1,FileSize(f1)-$80);<br>&nbsp; &nbsp; &nbsp; Truncate(f1)<br>&nbsp; &nbsp; end;<br>&nbsp; CloseFile(f1);<br>&nbsp; Result:=0;<br>end;<br><br>end.<br>
 
谢谢您如此详细的回答,只是还有几点疑问,请看下面的注释:<br><br>严黎斌 //他是谁?<br>unit ID3Kernel;<br><br>interface<br><br>type<br>&nbsp; Tid3v1= record<br>&nbsp; &nbsp; Tag: array[0..2] of char; &nbsp; &nbsp; &nbsp;//00..02 , ='TAG'<br>&nbsp; &nbsp; Title:array[0..$1d] of char; &nbsp;//03..20 &nbsp; &nbsp;$1d 是什么意思?<br>&nbsp; &nbsp; Artist:array[0..$1d] of char; &nbsp;//21..3e &nbsp; $1d是什么意思?<br>&nbsp; &nbsp; Album:array[0..$1d] of char; &nbsp;//3f..5c<br>&nbsp; &nbsp; Year:array[0..3] of char; &nbsp; &nbsp; &nbsp;//5d..60<br>&nbsp; &nbsp; Comment:array[0..$1c] of char; //61..7d &nbsp; $1c是什么意思?<br>&nbsp; &nbsp; Track:byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//7e &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; Genre:byte; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//7f &nbsp; &nbsp; 这两个注释是什么意思?<br>&nbsp; end;<br><br>function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>function DeleteID3v1(strFile:string):integer;<br><br>implementation<br><br>function ReadID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte; &nbsp; &nbsp; //$7f 是什么意思?<br>&nbsp; i: integer;<br>begin<br>&nbsp; result:=1;<br>&nbsp; if strFile='' then exit;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=0;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit; &nbsp; &nbsp; &nbsp;//$80 是什么意思?<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $7f do Read(f1,bytAll);<br>&nbsp; if (bytAll[0]&lt;&gt;ord('T')) and (bytAll[1]&lt;&gt;ord('A'))<br>&nbsp; &nbsp; and (bytAll[2]&lt;&gt;ord('G')) then exit; // no 'TAG' found<br>&nbsp; Move(bytAll,pid3v1,$80);<br>&nbsp; CloseFile(f1);<br>&nbsp; result:=0;<br>end;<br><br>function WriteID3v1(strFile:string;var pid3v1:Tid3v1):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte;<br>&nbsp; i: integer;<br>begin<br>&nbsp; result:=1;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=2;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit;<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $2 do Read(f1,bytAll); // test if 'TAG' exists<br>&nbsp; if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))<br>&nbsp; &nbsp; &nbsp; &nbsp; and (bytAll[2]=ord('G'))<br>&nbsp; &nbsp; then Seek(f1,FileSize(f1)-$80)<br>&nbsp; &nbsp; else Seek(f1,FileSize(f1));<br>&nbsp; Move(pid3v1,bytAll,$80);<br>&nbsp; for i:=0 to $7f do Write(f1,bytAll);<br>&nbsp; CloseFile(f1);<br>&nbsp; result:=0;<br>end;<br><br>function DeleteID3v1(strFile:string):integer;<br>var<br>&nbsp; f1:file of byte;<br>&nbsp; bytAll: array [0..$7f] of byte;<br>&nbsp; i: integer;<br>begin<br>&nbsp; Result:=1;<br>&nbsp; AssignFile(f1,strFile);<br>&nbsp; FileMode:=2;<br>&nbsp; Reset(f1);<br>&nbsp; if FileSize(f1)&lt;=$80 then exit;<br>&nbsp; Seek(f1, FileSize(f1)-$80);<br>&nbsp; for i:=0 to $2 do Read(f1,bytAll); // test if 'TAG' exists<br>&nbsp; if (bytAll[0]=ord('T')) and (bytAll[1]=ord('A'))<br>&nbsp; &nbsp; &nbsp; &nbsp; and (bytAll[2]=ord('G'))<br>&nbsp; &nbsp; then begin<br>&nbsp; &nbsp; &nbsp; Seek(f1,FileSize(f1)-$80);<br>&nbsp; &nbsp; &nbsp; Truncate(f1)<br>&nbsp; &nbsp; end;<br>&nbsp; CloseFile(f1);<br>&nbsp; Result:=0;<br>end;<br><br>end.<br>
 
1:$表示十六进制的数,所以$1d=16+13=29。。。其他的类似。<br>2:严黎斌,是这个单元的作者,在CSDN有一个注册的账号。
 
搞了半天$1d都不知道
 
To pcexplorer: 知识会有漏洞的嘛,我从未用十六进制表示过数当然不知道了。<br>To Kingron: &nbsp;谢谢!
 
顶部