将TList保存成文件

//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Dialogs.hpp>
#include <stdio.h>

//---------------------------------------------------------------------------
typedef struct
{
 AnsiString name;
 AnsiString id;
}Wtest;
class TForm1 : public TForm
{
__published: // IDE-managed Components
 TMemo *Memo1;
 TButton *Button1;
 TButton *Button2;
 TEdit *Edit1;
 TSaveDialog *SaveDialog1;
 TOpenDialog *OpenDialog1;
 TButton *Button3;
 TButton *Button4;
 TButton *Button5;
 void __fastcall Button1Click(TObject *Sender);
 void __fastcall Button2Click(TObject *Sender);
 void __fastcall FormShow(TObject *Sender);
 void __fastcall Button3Click(TObject *Sender);
 void __fastcall Button4Click(TObject *Sender);
 void __fastcall Button5Click(TObject *Sender);
private: // User declarations
 TList* WLst;
 TList* WLst1;
 Wtest* FTest;
 Wtest* FTest1;
 bool __fastcall SaveConfig();
 bool __fastcall ReadConfig();
public:  // User declarations
 __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

//---------------------------------------------------------------------------

#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::Button1Click(TObject *Sender)
{
    // Decalre a pointer to TFileStream
    TFileStream *FStream;

  // Let the user call the Save Dialog
    if( SaveDialog1->Execute() )
    {
        // Use the constructor of the TFileStream to create a file
        try {
            FStream = new TFileStream(SaveDialog1->FileName, fmCreate);
      // In the pointer to FStream, add the contents of the Memo
      FStream->Write(&WLst,sizeof(WLst));
      FStream->WriteComponent(Memo1);
            // and the content of the Edit controls
            FStream->WriteComponent(Edit1);
        }
        __finally
    {
            // Since the pointer was created, delete it,
            // whether it was used or not
            delete FStream;
        }
    }
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    TFileStream *FStream;

    if( OpenDialog1->Execute() )
    {
        try {
            FStream = new TFileStream(OpenDialog1->FileName,
                                      fmOpenRead | fmShareExclusive);
      FStream->ReadComponent(Memo1);
      FStream->ReadComponent(Edit1);
        }
        __finally
        {
      delete FStream;
        }
    }

}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
 WLst = new TList;
 WLst1 = new TList;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
 FTest = new Wtest;
 FTest1= new Wtest;
 FTest->name = "yar";
 FTest->id ="1023";
 WLst->Add(FTest);

}
//---------------------------------------------------------------------------
//save文件中
void __fastcall TForm1::Button4Click(TObject *Sender)
{
 TMemoryStream* ms = new TMemoryStream;
 TStringList* WStr = new TStringList;
 try
 {
  ms->WriteBuffer(&(WLst->Count),sizeof(int));
  for (int i = 0; i < WLst->Count; i++) {
  ShowMessage(((Wtest*)(WLst->Items[i]))->name);
   ms->WriteBuffer((Wtest*)(WLst->Items[i]),sizeof(Wtest));
  }
  ms->Position =0;
  ms->SaveToFile("D://1.txt");
 }
 __finally
 {
  ms->Free();
 }
}
//从文件中读出来
void __fastcall TForm1::Button5Click(TObject *Sender)
{
 TMemoryStream* ms1 = new TMemoryStream;
 try
 {
  int num;
  ms1->LoadFromFile("D://1.txt");
  ms1->ReadBuffer(&num,sizeof(int));
  for (int i = 0; i < num; i++) {
   FTest1= new Wtest;
   WLst1->Add(FTest1);
   ms1->ReadBuffer((Wtest*)(WLst1->Items[i]),sizeof(Wtest));
   ShowMessage(((Wtest*)(WLst1->Items[i]))->name);
  }
   ShowMessage(IntToStr(num));
 }
 __finally
 {
  delete ms1;
 } 
}
//---------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值