抱歉,前面的代码没有调试过,现在的是调试过的了:
//.cpp文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReadButtonClick(TObject *Sender)
{
myStruct st;
int rs;
rs = ReadDataPack(0, &st, "C://temp//StruData.dat");
if (rs<0)
ShowMessage("读取数据文件失败!");
else
ShowMessage(IntToStr(st.one)+","+IntToStr(st.Two)+","+IntToStr(st.three));
}
//---------------------------------------------------------------------------
int __fastcall TForm1::AddDataPack(myStruct *Data, AnsiString FileName)
{
TFileStream *fs;
int rs,StruSize;
StruSize = sizeof(myStruct);
try{
fs = new TFileStream(FileName, fmCreate + fmOpenReadWrite);
//将数据写到文件尾部
fs->Seek(0, soFromEnd);
fs->WriteBuffer(Data, StruSize);
delete fs;
rs = 0;
}catch(Exception &E){
rs = -1;
}
return rs;
}
int __fastcall TForm1::ReadDataPack(int Index, myStruct *Data, AnsiString FileName)
{
TFileStream *fs;
int rs,StruSize;
StruSize = sizeof(myStruct);
try{
fs = new TFileStream(FileName, fmOpenRead);
//读取相应位置的数据
fs->Seek(StruSize*Index, soFrombegin
ning);
fs->ReadBuffer(Data, StruSize);
delete fs;
rs = 0;
}catch(Exception &E){
rs = -1;
}
return rs;
}
void __fastcall TForm1::WriteButtonClick(TObject *Sender)
{
myStruct st;
int rs;
st.one = 1;
st.Two = 2;
st.three = 3;
rs = AddDataPack(&st, "C://temp//StruData.dat");
if (rs<0)
ShowMessage("写数据包失败!");
else
ShowMessage("成功写入数据包!");
}
//---------------------------------------------------------------------------
int __fastcall TForm1::LoadAllData(AnsiString FileName)
{
TFileStream *fs;
myStruct *st;
int StruSize,iCount,rs;
StruSize = sizeof(myStruct);
try{
//打开文件
fs = new TFileStream(FileName, fmOpenRead);
//确定文件中存了多少个结构
iCount = fs->Size/StruSize;
//为存放数据的动态数组分配内存
st = new myStruct[iCount];
//读出数据
fs->ReadBuffer(st, fs->Size);
//在此加入使用各个结构值的代码:
ListBox1->Items->Clear();
for (int i=0;
i<iCount;
i++){
ListBox1->Items->Add(IntToStr(st.one)+","+IntToStr(st.Two)+","+
IntToStr(st.three));
}
delete fs;
delete[] st;
rs = 0;
}catch(Exception &E){
rs = -1;
}
return rs;
}
void __fastcall TForm1::LoadAllClick(TObject *Sender)
{
int rs;
rs = LoadAllData("C://temp//StruData.dat");
if (rs<0)
ShowMessage("读取数据失败!");
}
//---------------------------------------------------------------------------
//.h文件
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
typedef struct _myStruct
{
ULONG one;
BYTE Two:4;
BYTE three:4;
BYTE four;
USHORT five;
union
{
BYTE six[20];
ULONG seven;
} myUnion;
}myStruct;
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *ReadButton;
TButton *WriteButton;
TListBox *ListBox1;
TButton *LoadAll;
void __fastcall ReadButtonClick(TObject *Sender);
void __fastcall WriteButtonClick(TObject *Sender);
void __fastcall LoadAllClick(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
int __fastcall AddDataPack(myStruct *Data, AnsiString FileName);
int __fastcall ReadDataPack(int Index, myStruct *Data, AnsiString FileName);
int __fastcall LoadAllData(AnsiString FileName);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif