读文件到动态数组老不成功,静态就可以,奇怪。 ( 积分: 100 )

  • 主题发起人 主题发起人 留香客
  • 开始时间 开始时间

留香客

Unregistered / Unconfirmed
GUEST, unregistred user!
读文件到动态数组老不成功,静态就可以,奇怪。
Var
res : boolean;
bytestoread, numread: longword;
handle : THandle;
a : Array Of byte;
buf : Pointer;
t : TFileStream;
Begin
SetLength(a,10000);
handle := CreateFile(pchar('c:/1.rar'), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
Nil, OPEN_EXISTING,0, 0);
bytestoread := 10000;
res := ReadFile(handle, a,bytestoread, numread, Nil);
t := TFileStream.Create('c:/1.txt', fmCreate Or fmOpenWrite);
t.WriteBuffer(a, 10000);
t.Free;
CloseHandle(handle);

end;
 
res := ReadFile(handle, Pointer(a)^,bytestoread, numread, Nil);

t.WriteBuffer(Pointer(a)^, 10000);
 
出错了
显示:Stream write error.
如果换成静态数组就可以了。
a:ayyay[0..999] of byte;
但是我不想这样:-(
还有Pointer(a)^不是等于a吗?
 
t.WriteBuffer(a[0], 10000);
 
或者是
t.WriteBuffer(PChar(@a[0])^, 10000);
 
function ReadFile
If the function succeeds, the return value is nonzero.
If the return value is nonzero and the number of bytes read is zero, the file pointer was beyond the current end of the file at the time of the read operation. However, if the file was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the return value is FALSE and GetLastError returns ERROR_HANDLE_EOF when the file pointer goes beyond the current end of file.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
 
t.WriteBuffer(PChar(@a[0])^, 10000);
 
不行 文件全不是0了
最主要的问题是“a”,是静态还是动态数组,直接影响了结果。
不知到为什么。
如果把a定义a:ayyay[0..999] of byte;
就完全没有问题了,但是我想定义动态数组
 
动态数组在使用之前不需要设置其容量吗?
 
需要的,请看源程序,已经设置过了。
是不是因为array of声明的动态数组是一个指针?
 
没有像你这样搞的

要动态就这样

VAR
s:pchar;//或其它指针类型
begin
getmem(s,1000);
.........
freemem(s);
end;
 
http://22627167.qzone.qq.com
动态数组
 
to:41426277
你的方法我试过的,没有效果,一样
Var
res : boolean;
bytestoread, numread: longword;
handle : THandle;
a : Pchar;
t : TFileStream;
Begin
GetMem(a, 10000);
handle := CreateFile(pchar('c:/1.rar'), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
Nil, OPEN_EXISTING, 0, 0);
Try
bytestoread := 10000;
res := ReadFile(handle, a, bytestoread, numread, Nil);

t := TFileStream.Create('c:/1.txt', fmCreate Or fmOpenWrite);
t.WriteBuffer(a, 10000);
t.Free;
Finally
CloseHandle(handle);
FreeMem(a);
End;
End;
 
Var
res : boolean;
bytestoread, numread: longword;
handle : THandle;
a : Pchar;
t : TFileStream;
Begin
GetMem(a, 10000);
handle := CreateFile(pchar('c:/1.rar'), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
Nil, OPEN_EXISTING, 0, 0);
Try
bytestoread := 10000;
res := ReadFile(handle, a^, bytestoread, numread, Nil);

t := TFileStream.Create('c:/1.txt', fmCreate Or fmOpenWrite);
t.WriteBuffer(a^, 10000);
t.Free;
Finally
CloseHandle(handle);
FreeMem(a);
End;
End;


Var
res : boolean;
bytestoread, numread: longword;
handle : THandle;
a : array of byte;
t : TFileStream;
Begin
SetLength(a, 10000);
ZeroMemory(@a[0], Length(a));
handle := CreateFile(pchar('c:/1.rar'), GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE,
Nil, OPEN_EXISTING, 0, 0);
Try
bytestoread := 10000;
res := ReadFile(handle, pointer(@a[0])^, bytestoread, numread, Nil);

t := TFileStream.Create('c:/1.txt', fmCreate Or fmOpenWrite);
t.WriteBuffer(pointer(@a[0])^, 10000);
t.Free;
Finally
CloseHandle(handle);
End;
End;
 
写成:
res := ReadFile(handle, PChar(a)^,bytestoread, numread, Nil);
...
t.WriteBuffer(PChar(a)^, 10000);
或:
res := ReadFile(handle, a[0],bytestoread, numread, Nil);
...
t.WriteBuffer(a[0], 10000);
不要轻易反对,试试就知道了。
不过建议不要用动态数组,严重影响速度。
 
谢谢,TrustMe很好的解决了。
 
后退
顶部