答えて
は、あなたがこのような構造を記述するために、次の記録今
type
TMyRecord = record
FirstName: string[100]; // 100 characters max. for First name
LastName: string[100]; // 100 characters max. for Last name
Age: Byte;
DateOfBirth: TDateTime;
end;
const
// if you are using Delphi 2009 and above,
// then either change *string[100]* to *AnsiString[100]* or use a different
// approach to save the string, read bellow
szMyRecord = SizeOf(TMyRecord); // storing it will make your code run faster if you write a lot of records
を、持っていると仮定したデータのみ、無機能でありますストリームの場合は、次のようにする必要があります。
procedure WriteRecord(
const ARecord: TMyRecord;
const AStream: TStream // can be a TMemoryStream, TFileStream, etc.
);
begin
AStream.Write(ARecord, szMyRecord);
end;
FirstNameを "stするために、
type
TMyRecordWithVeryLongStrings = record
LenFirstName: Integer; // we store only the length of the string in this field
LenLastName: Integer; // same as above
Age: Byte;
DateOfBirth: TDateTime;
FirstName: string; // we will ignore this field when writing, using it for value
LastName: string; // same as above
end;
const
// we are ignoring the last two fields, since the data stored there is only a pointer,
// then we can safely assume that (SizeOf(string) * 2) is the offset
szMyRecordWithVeryLongStrings = SizeOf(TMyRecordWithVeryLongStrings) - (SizeOf(string) * 2);
// the difference between this method and above is that we first write the record
// and then the strings
procedure WriteRecord(
ARecord: TMyRecordWithVeryLongStrings;
AStream: TStream // can be a TMemoryStream, TFileStream, etc.
);
const szChar = sizeof(char);
begin
// ensure the length of first and Last name are stored in "Len + Name" field
ARecord.LenFirstName := Length(ARecord.FirstName);
ARecoord.LenLastName := Length(ARecord.Lastname);
// write the record
AStream.Write(ARecord, szMyRecordWithVeryLongStrings);
// write First name value
AStream.Write(
Pointer(ARecord.FirstName)^, // value of first name
szChar * ARecord.LenFirstName
);
// repeat as above for last name
AStream.Write(
Pointer(ARecord.LastName)^, // value of first name
szChar * ARecord.LenLastName
);
end;
今:リング文字列[100] 『あるいは、例えば、文字列フィールドを記述するための特別なメソッドを使用して」姓の文字は保存されません、あなたは私が行ったように姓を宣言する必要があります』 「長い文字列」を読んで、あなたが最初のレコードの読み取り:私は(それが役に立てば幸い
procedure ReadRecord(
ARecord: TMyRecordWithVeryLongStrings;
AStream: TStream
);
begin
AStream.Read(Arecord, szMyRecordWithVeryLongStrings);
// now read first and last name values which are right after the record in the stream
AStream.Read(Pointer(ARecord.FirstName)^, szChar * ARecord.LenFirstName);
AStream.Read(Pointer(ARecord.,LastrName)^, szChar * ARecord.LenLastName);
end;
を:
+1とそのような包括的な例のおかげで!私はそれをコピー/ペーストしてテストし、私がそれを働かせたらあなたに答えを与えるでしょう。ありがとう!! – Mawg
これは短い文字列(文字列[xx])でのみ機能することに注意してください。彼らはアンシだけです。他のすべての文字列はポインタであり、それらを書き込むためにはさらに多くの作業が必要です。 –
Delphi 2009以降、ショートストリングは廃止されました。これは、IMHOはレコードを保存するための良い選択肢ではなく、スペースを浪費します。 –
http://code.google.com/p/kblib/ - あなたは(一定の大きさに文字列を制限する必要がこれを試していない、と複雑で動作することができレコード、1行でロード/セーブ)。
同様の質問:
- 1. Deviareで構造体のフィールドに書き込む方法は?
- 2. 構造体charをファイルに書き込む方法
- 3. 文字列を含む構造体をバイナリファイルに書き込む
- 4. 配列を含む構造体をバイナリファイルに書き込む
- 5. 構造体からXamlページのテキストボックスにInt値を書き込む方法は?
- 6. 既存のバイト配列に構造体を書き込む方法は?
- 7. 構造体をファイルに書き込むときのアクセス違反
- 8. SwiftのoutputStreamに構造体を書き込む3
- 9. Swiftクラスに静的構造体を埋め込む方法は?
- 10. HDF5:別の構造体へのポインタを含む構造体を書き込む複合データ型
- 11. Cスタック可変長構造体を書き込むマクロ?
- 12. mysqlから適切なJSON構造体を書き込む
- 13. 書き込み構造体コード
- 14. 別のベクトルを含む構造体のベクトルをバイナリファイルに書き込む
- 15. 構造体のベクトルを読み込んでファイルに書き込む
- 16. バイトブロックを構造体に読み込む方法
- 17. Pythonでテーブル構造のデータをPDFファイルに書き込む方法は?
- 18. ディレクトリ構造全体を埋め込む
- 19. JSON構造体を書き直してRailsにEACHループを組み込む
- 20. Cで構造体をファイルに書き込む移植可能な方法は何ですか?
- 21. 構造体の配列をC言語のバイナリファイルに書き込む
- 22. 構造体に同じ名前の構造体を2つ埋め込む
- 23. 名前のある構造体を別の構造体に匿名構造体として埋め込む
- 24. C++:動的なデータを構造体にエレガントに読み込む方法は?
- 25. ostream ::構造体全体が書き込まれませんか?
- 26. 他の構造体を含むバイナリファイルから構造体を読み込んで読み込む(C)
- 27. バッファオブジェクトにストリームを書き込む
- 28. RCDATAリソースにストリームを書き込む
- 29. ディスクにMJPEGストリームを書き込む
- 30. JavaのmongoDBにストリームを書き込む
[ファイルへの複雑なレコードの書き込み]の可能な重複(http://stackoverflow.com/questions/4533376/執筆 - 複合 - レコード - ファイル) –