我想从文件里读出数据放到数组里。就这么简单,结果下段程序错了,能告诉我错在哪里吗?
没多少可用分了,我这几天挣到了再加,急。
type
TAArray = array of Integer;
end;procedure TForm1.Button1Click(Sender: TObject);
const
TMP_COUNT=50;
var
m: array[0..999] of TAArray;
i: Integer;
sFile:string;
begin
sFile:='C:\WINDOWS\system32\x.xx';
for i := Low(m) to High(m) do SetLength(m[i], TMP_COUNT);
if FileExists(sFile) then
LoadDatafromFile(sFile, m[0][0], Length(m) * TMP_COUNT * SizeOf(Integer));
end;procedure TForm1.LoadDatafromFile(const AFileName: string; var AData; const ASize: Integer);
const
BLOCK_SIZE = 1024;
var
BytesRead, BytesToWrite, Count: Integer;
F: file of Byte;
pTemp: Pointer;
mData: array of Byte;
begin
ZeroMemory(@AData, ASize); ///////////////////错
AssignFile(F, AFileName);
Reset(F);
try
Count := FileSize(F);
setlength(mData, Count);
pTemp := @mData[0];
BytesRead := BLOCK_SIZE;
while (BytesRead = BLOCK_SIZE) do
begin
BytesToWrite := Min(Count, BLOCK_SIZE);
BlockRead(F, pTemp^, BytesToWrite, BytesRead);
pTemp := Pointer(Longint(pTemp) + BLOCK_SIZE);
Count := Count - BytesRead;
end;
finally
CloseFile(F);
end;
Move(mData[0], AData, ASize); ///////////////////////////错
end;
没多少可用分了,我这几天挣到了再加,急。
type
TAArray = array of Integer;
end;procedure TForm1.Button1Click(Sender: TObject);
const
TMP_COUNT=50;
var
m: array[0..999] of TAArray;
i: Integer;
sFile:string;
begin
sFile:='C:\WINDOWS\system32\x.xx';
for i := Low(m) to High(m) do SetLength(m[i], TMP_COUNT);
if FileExists(sFile) then
LoadDatafromFile(sFile, m[0][0], Length(m) * TMP_COUNT * SizeOf(Integer));
end;procedure TForm1.LoadDatafromFile(const AFileName: string; var AData; const ASize: Integer);
const
BLOCK_SIZE = 1024;
var
BytesRead, BytesToWrite, Count: Integer;
F: file of Byte;
pTemp: Pointer;
mData: array of Byte;
begin
ZeroMemory(@AData, ASize); ///////////////////错
AssignFile(F, AFileName);
Reset(F);
try
Count := FileSize(F);
setlength(mData, Count);
pTemp := @mData[0];
BytesRead := BLOCK_SIZE;
while (BytesRead = BLOCK_SIZE) do
begin
BytesToWrite := Min(Count, BLOCK_SIZE);
BlockRead(F, pTemp^, BytesToWrite, BytesRead);
pTemp := Pointer(Longint(pTemp) + BLOCK_SIZE);
Count := Count - BytesRead;
end;
finally
CloseFile(F);
end;
Move(mData[0], AData, ASize); ///////////////////////////错
end;
解决方案 »
- DBNavigator按扭enable调用
- msde如何支持bcp
- Delphi7中的corba问题,非高手不能解决!先谢了。
- 关于dephi数据库组件的讨论
- Delphi报表另存为.xls或.rtf格式时连线和图片都不可见,为什么?
- 怎样得到一个指针记录的大小???
- 请问谁有ic卡类的资料或原程序什么的啊???
- 我100分问一个关于IDHTTP POST后收不到重新定向的地址的问题!
- 我下载了一个控件叫XPMenu.pas的菜单资源文件,我不清楚如何使用才能使用这种没有具体图形的原代码?
- 请教高手:如何把一个数据库的两个字段分别设置成Tdbchart的横坐标和纵坐标,急用呀~~急用呀~~急用呀~~
- zdb后缀名的文件用什么软件打开啊。
- 不明白 scanline的出问题;我估计是指针引起的;但是找不出解决方法;help!~~~~~~~
这不是分配了吗
m[0] , m[1]...可能不在一个连续的空间里,array of array时,其实是一个指针数组,因此分配的内存完全可能不连续
procedure TForm1.Button1Click(Sender: TObject);
var
fmem: TFileStream;
arrByte: TBytes;
begin
fmem := TFileStream.Create('e:\1.txt',fmOpenRead);
setlength(arrbyte,fmem.Size);
fmem.Read(arrbyte[0],fmem.Size);
fmem.Free;
end;
type
Tfoo = record
Values : array of Integer;
m : array[0..999]of PIntegerArray;
end;procedure TForm1.Button1Click(Sender: TObject);
const
TMP_COUNT=50;
var
bar: Tfoo;
i: Integer;
begin
SetLength(bar.Values, TMP_COUNT*Length(bar.m));
for i:=0 to High(bar.m) do
bar.m[i] := @bar.Values[i*TMP_COUNT];
with TFileStream.Create(sFile, fmOpenRead) do
try
Read(bar.Values[0], Length(bar.Values) * SizeOf(bar.Values[0]))
// or:
//Read(bar.m[0][0], Length(bar.m)*TMP_COUNT * SizeOf(bar.Values[0]))
finally
Free
end;
end;