掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
Single型のポインターの使い方 (ID:47638)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
で…、yTake さんの目的が「ファイルからデータを二次元動的配列に読み出したい」のか、 「ポインタの使用方法を知りたい」のか、どちらか分からなかったので、全部お伝えしておきます。 // TFileStreamで1行毎に読み出す procedure single_array_read_1(binary_f: String; xx, yy: Integer); var mtx: array of array of Single; FS: TFileStream; I: Integer; Size: Integer; begin SetLength(mtx, yy, xx); FS:=TFileStream.Create(binary_f, fmOpenRead or fmShareDenyWrite); try for I:=Low(mtx) to High(mtx) do begin Size:=SizeOf(mtx[I][0])*Length(mtx[I]); FS.ReadBuffer(Pointer(mtx[I])^, Size); end; finally Fs.Free; end; // 読み出したデータを処理 end; // TMemoryStreamでファイルを全部Loadしておき、1行毎に読み出す procedure single_array_read_2(binary_f: String; xx, yy: Integer); var mtx: array of array of Single; MS: TMemoryStream; I: Integer; Size: Integer; begin SetLength(mtx, yy, xx); MS:=TMemoryStream.Create; MS.LoadFromFile(binary_f); try for I:=Low(mtx) to High(mtx) do begin Size:=SizeOf(mtx[I][0])*Length(mtx[I]); MS.ReadBuffer(Pointer(mtx[I])^, Size); end; finally MS.Free; end; // 読み出したデータを処理 end; // あえてポインタを使ってみる(ただし無駄な処理が増えます) procedure single_array_read_3(binary_f: String; xx, yy: Integer); var mtx: array of array of Single; Size: Integer; P: Pointer; pValue: PSingle; FS: TFileStream; I, J: Integer; begin SetLength(mtx, yy, xx); Size:=SizeOf(pValue)*xx*yy; GetMem(P, Size); pValue:=P; try FS:=TFileStream.Create(binary_f, fmOpenRead or fmShareDenyWrite); try FS.ReadBuffer(pValue^, Size); finally Fs.Free; end; for I:=Low(mtx) to High(mtx) do begin for J:=Low(mtx[I]) to High(mtx[I]) do begin mtx[I][J]:=pValue^; Inc(pValue); end; end; finally FreeMem(P); end; // 読み出したデータを処理 end; なお私は普段GetMemとかまったく使いませんので、ポインタを使った例が適切か自信がありません。 もしツッコミ等ありましたら、どなたかよろしく。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.