一个比较简单的问题(关于文件读写)(50分)

  • 主题发起人 主题发起人 delphilike
  • 开始时间 开始时间
D

delphilike

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一个二维的数组T[1..64,1..64]<br>现在需要将其以一个矩阵的形式存入一个文件中<br>不知道如何操作?<br>请赐教
 
我觉得你怎么保存都无所谓,关键是你读出来之后赋值给数组就可以了.
 
我的意思是我生成一个重要的数组<br>我需要将其保存为一个文件供其他程序调用
 
with TFileStream.Create('aa.dat',fmCreate) do<br>begin<br>&nbsp; Write(@T,Sizeof(T)); //写<br>&nbsp; Free;<br>end;<br><br>with TFileStream.Create('aa.dat',fmCreate) do<br>begin<br>&nbsp; Read(@T,Sizeof(T)); //读<br>&nbsp; Free;<br>end;<br><br>&nbsp;
 
pat1:<br><br>with TFileStream.Create('aa.dat',fmOpenRead) do<br>begin<br>&nbsp; Read(@T,Sizeof(T)); //读<br>&nbsp; Free;<br>end;<br>
 
&gt;&gt;保存为一个文件供其他程序调用<br>一样啊! 既然保存格式你自己知道,不管在哪个程序里,你自己读出来并赋值给数组不就行了?
 
begin &nbsp;<br>Write(@T,Sizeof(T)); //写 &nbsp;<br>Free;<br>出错显示Varible required
 
var<br>&nbsp; ar:array[0..4,0..4] of char;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; i,j:integer;<br>&nbsp; f:file;<br>begin<br>&nbsp; for i:=0 to 4 do<br>&nbsp; &nbsp; for j:=0 to 4 do<br>&nbsp; &nbsp; &nbsp; ar[j]:=char(ord('a')+i+j);<br>&nbsp; assignfile(f,'test.dat');<br>&nbsp; rewrite(f,1);<br>&nbsp; blockwrite(f,ar,25);<br>&nbsp; closefile(f);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; i,j:integer;<br>&nbsp; f:file;<br>begin<br>&nbsp; assignfile(f,'test.dat');<br>&nbsp; reset(f,1);<br>&nbsp; blockread(f,ar,25);<br>&nbsp; closefile(f);<br>&nbsp; for i:=0 to 4 do<br>&nbsp; &nbsp; for j:=0 to 4 do<br>&nbsp; &nbsp; &nbsp; memo1.lines.add(ar[j]);<br>end;<br>
 
Write(@T,Sizeof(T)); <br>改成<br>Write(T[0,0],Sizeof(T));
 
保存到Ini,从Ini读出:<br><br>procedure WriteToIni(const iniFileName:string;const Section:string);<br>var<br>&nbsp; iniFile:TIniFile;<br>&nbsp; iCol,iRow:integer;<br>&nbsp; s:string;<br>begin<br>&nbsp; if(Section = '') then exit;<br><br>&nbsp; IniFile := TIniFile.create(ExtractFilePath(ParamStr(0))+ iniFileName);<br><br>&nbsp; if(iniFile = nil) then exit;<br><br>&nbsp; iniFile.EraseSection(Section);<br><br>&nbsp; for iRow := 1 to 64 do<br>&nbsp; begin<br>&nbsp; &nbsp; for iCol := 1 to 64 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s := IntToStr(iCol) + '_' + IntToStr(iRow);<br>&nbsp; &nbsp; &nbsp; iniFile.WriteInteger(Section,s,T[iCol,iRow]);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>&nbsp; iniFile.Free;<br>end;<br><br>procedure ReadFromIni(const iniFileName:string;const Section:string);<br>var<br>&nbsp; iniFile:TIniFile;<br>&nbsp; i,iPos,iCol,iRow:integer;<br>&nbsp; name,value:string;<br>&nbsp; values:TStringList;<br>begin<br>&nbsp; if(Section = '') then exit;<br><br>&nbsp; IniFile := TIniFile.create(ExtractFilePath(ParamStr(0))+ iniFileName);<br><br>&nbsp; if(iniFile = nil) then exit;<br><br>&nbsp; values := TStringList.Create;<br><br>&nbsp; iniFile.ReadSectionValues(Section,values);<br>&nbsp; iniFile.Free;<br><br>&nbsp; for i:= 0 to values.Count - 1 do<br>&nbsp; begin<br>&nbsp; &nbsp; iPos := Pos('=',values);<br>&nbsp; &nbsp; name := Copy(values,1,iPos - 1);<br>&nbsp; &nbsp; value := Copy(values,iPos + 1,Length(values) - iPos);<br><br>&nbsp; &nbsp; iPos := Pos('_',name);<br>&nbsp; &nbsp; iCol := StrToInt(Copy(name,1,iPos - 1));<br>&nbsp; &nbsp; iRow := StrToInt(Copy(name,iPos + 1,Length(name) - iPos));<br><br>&nbsp; &nbsp; T[iCol,iRow] := StrToInt(value);<br>&nbsp; end;<br><br>&nbsp; values.Free;<br>end;<br>
 
跟Hexi和cAKK的比,比较笨拙了,呵呵。<br>我原来是用来保存TStringGrid的数据的。<br>在这个问题上看来不用那么复杂。
 
将T以var形式传给读写过程。
 
越看程序越长. ^_^<br><br>其实用文本文件方式, 加上write, writeln的方法不就可以了么?<br><br>{假设是10行5列的矩阵, 数据类型是integer}<br>const m=10;<br>&nbsp; &nbsp; &nbsp; n=5;<br><br>var a:array[0..m-1, 0..n-1] of integer;<br>&nbsp; &nbsp; t:text;i, j:integer;<br><br>begin<br><br>&nbsp; &nbsp; {$I-}<br>&nbsp; &nbsp; assignfile(t, 'C:/Tempfile.aaa');<br>&nbsp; &nbsp; rewrite(t);<br>&nbsp; &nbsp; {$I+}<br>&nbsp; &nbsp; if ioresult&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; {文件创建错误}<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br><br>&nbsp; &nbsp; {$I-}<br>&nbsp; &nbsp; for i:=0 to m-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; for j:=0 to n-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; write(t, a[i, j], ' ');<br>&nbsp; &nbsp; &nbsp; &nbsp; writeln(t);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; {$I+}<br>&nbsp; &nbsp; closefile(t);<br><br>end;<br><br>读取的话也不复杂, 直接用read, readln就可以了<br><br>const m=10;<br>&nbsp; &nbsp; &nbsp; n=5;<br><br>var a:array[0..m-1, 0..n-1] of integer;<br>&nbsp; &nbsp; t:text;i, j:integer;<br><br>begin<br><br>&nbsp; &nbsp; {$I-}<br>&nbsp; &nbsp; assignfile(t, 'C:/Tempfile.aaa');<br>&nbsp; &nbsp; reset(t);<br>&nbsp; &nbsp; {$I+}<br>&nbsp; &nbsp; if ioresult&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; {文件创建错误}<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br><br>&nbsp; &nbsp; {$I-}<br>&nbsp; &nbsp; for i:=0 to m-1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; for j:=0 to n-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; read(t, a[i, j]);<br>&nbsp; &nbsp; &nbsp; &nbsp; readln(t);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; {$I+}<br>&nbsp; &nbsp; closefile(t);<br><br>end;<br><br>如果是C的话, 用fscanf代替read也是一样的.
 
我觉得无论如何实现,应该突出面向对象的思想.
 
前些日子市面上有一套双CD的程序员大本营,里面列出了DELPHI中关于<br>文件处理的API函数的调用.你如果需要我可以MAIL给你.OK?<br>另外,当你知道了一个相关的函数名称,例如TIniFile.create,在DELPHI的<br>环境中将其选中,按F1键,出来HELP后选择相关函数后,慢慢的找你所要的函数吧
 
我觉得Iknow的最好,其实面向对象只是让人觉得简单,<br>所以怎么简单怎么来。
 
多人接受答案了。
 
后退
顶部