我想要从一个文件中 读出标准头32字节
为何出错了!
代码如下:
const SIGNNAME_LENGTH=32;nReal:Longint;
b:Cardinal;
SignName: array [0..SIGNNAME_LENGTH-1] of byte;
b:=ReadFile(hFile,@SignName,SIGNNAME_LENGTH,@nReal,nil); //(出错行,hfile是createfile的返回)
为何出错了!
代码如下:
const SIGNNAME_LENGTH=32;nReal:Longint;
b:Cardinal;
SignName: array [0..SIGNNAME_LENGTH-1] of byte;
b:=ReadFile(hFile,@SignName,SIGNNAME_LENGTH,@nReal,nil); //(出错行,hfile是createfile的返回)
解决方案 »
- 程序连接服务器, 如何判断用户是电信或网通?
- Windows多任务处理机制对串口通信的影响
- ADOQuery 参数传递问题?
- 着急;怎么让MDI子窗口Showmodal出来的窗口不在任务栏产生图标
- datacontrol中有没有类似于editmask的控件?若没有如何实现输入规格化??
- 我想创建一张VFP格式的数据表格
- 2002年Delphi Informant杂志评出的读者选择奖项
- 如何多线程同时写一个TfileStream文件??
- 我建的一个project打不开了,每次打开时就会导致delphi6关闭,谁能告诉我这是怎么回事?
- 如何判断客户机是否在线上???
- 那位朋友有好看点的软件界面?
- 开发一个解压缩程序
var lpNumberOfBytesRead: DWORD; lpOverlapped: POverlapped): BOOL; stdcall;
http://www.emutalk.net/archive/index.php/t-4009.htmlCybermanApril 7th, 2002, 04:44
Originally posted by Mayco
I have a problem: when I use the readfile api function in delphi it returns everything what i need except the first byte (when the file begins with "123..." it just returns "23...").This is the piece of code I use:procedure openfile();
var
dwread:LongWord;
hFile: THandle;
data: Shortstring;
begin
hFile := createfile(PChar('c:\file.bin'), GENERIC_READ, FILE_SHARE, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL + FILE_FLAG_RANDOM_ACCES, 0);
SetFilePointer(hFile, 0, nil, FILE_BEGIN);
Readfile(hFile, data, 64, dwread, nil);
Closehandle(hFile);
end;c:\file.bin is the file I want to read of course.
data must contain the first 64 bytes of a binary file but it doesn't add the first byte.
I suggest not setting the file pointer the first time by commenting it out. Then run the code. Also if you have a 'file' position function I would dump that information to a debug text object in some window of your application. Useful for figuring out such things.Cyb
ReadFile前先用SetFilePointer,你引用参数又不正确,将@SignName,@nReal的@去掉
var
Result: Longint;
bKernelExist,nStrategyCount,nKernelStrategyCount: Longint;
reportFlow:array [0..8191] of byte;
pReportFlow:PByte;
nDefaultStrategyCookie: Longint;
Password:array [0..6] of byte;
pPassword:PByte;
pParamCheck:Longint;
hFile:Integer;
nFlowID:Longint;
pBuf,pBufTemp:PByte;
i:Integer;
pTEFSInfoFlowLen:Longint;
nFileSize,nReal,b:Longint;
SignName: array [0..SIGNNAME_LENGTH-1] of byte;
pSignName:pchar;
begin
OpenDialog1.Execute;
if trim(OpenDialog1.FileName)<>'' then begin
showmessage(OpenDialog1.FileName);
result:=DSF_Driver_FileCanServe(pchar(OpenDialog1.FileName));
if result<>1 then exit;
hFile:=DSF_Driver_CreateFile(pParamCheck,pchar(OpenDialog1.FileName),GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, 0);
nFileSize:= GetFileSize(hfile,nil);
if (nFileSize=0) and ((nFileSize mod TEFS_ALIGN_CONST)<>0) then exit;//不为0,或是不是256的倍数,一定不是加密文件
//加密文件的尾部是配置块,配置块的最后32字节是签名
b:=SetFilePointer(hFile,-SIGNNAME_LENGTH,nil,FILE_END); //seek到要求的位置
if (b<>(nFileSize-SIGNNAME_LENGTH)) then begin CloseHandle(hFile);exit;end; //出错
//读出标准头32字节
b:=ReadFile(hFile,SignName,SIGNNAME_LENGTH,nReal,nil); //从原文件中读 end;
const SIGNNAME_LENGTH=32;
var
SignName: array [0..SIGNNAME_LENGTH-1] of byte;
hFile:THandle;
nReal:DWORD;
i:integer;
Result:string;
begin
hFile:=CreateFile(PChar('c:\1.mp3'),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,0);
if ReadFile(hFile,SignName,SIGNNAME_LENGTH,nReal,nil) then;
for i:=0 to SIGNNAME_LENGTH-1 do
Result:=Result+Format('%2.2x ',[SignName[i]]);
ShowMessage(Result);
end;