1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString nom[2];
int fic;
char name[200],name1[200];
nom[0]=ChangeFileExt(Application->ExeName,".lst");
nom[1]=ChangeFileExt(Application->ExeName,".bak");
lstrcpy(name,nom[0].c_str());
lstrcpy(name1,nom[1].c_str());
if (FileExists(name))
{
DeleteFile(name1);
RenameFile(name,name1);
}
fic=FileCreate(name);
int x;
if (fic!=-1)
{
x=ListeChanson->Count;
FileWrite(fic,&x,sizeof(int));
long RecLength;
for (int i=0;i<ListeChanson->Count;i++)
{
PtCHANSON=(CHANSON *)ListeChanson->Items[i];
RecLength=ComputeSize(*PtCHANSON);
FileWrite(fic,&RecLength,sizeof(long));
FileWrite(fic,PtCHANSON->titre.c_str(),PtCHANSON->titre.Length()+1);
FileWrite(fic,PtCHANSON->chemin.c_str(),PtCHANSON->chemin.Length()+1);
FileWrite(fic,&PtCHANSON->longueur,sizeof(PtCHANSON->longueur));
}
FileClose(fic);
}
ClearMemor();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearMemor()
{
for(int n=0;n<ListeChanson->Count;n++)
delete ListeChanson->Items[n];
ListeChanson->Clear();
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
long RecLength;
AnsiString nom;
int fic,x;
char name[200];
ClearMemor();
nom=ChangeFileExt(Application->ExeName,".lst");
lstrcpy(name,nom.c_str());
if (FileExists(name))
{
// Ouvrir le fichier
fic=FileOpen(name,fmOpenRead);
FileRead(fic,&x,sizeof(int));
char* tmp;
for (int i=0;i<x;i++)
{
FileRead(fic,&RecLength,sizeof(long));
char *buffer =new char[RecLength];
tmp=buffer;
FileRead(fic,buffer,RecLength);
PtCHANSON=new CHANSON;
PtCHANSON->titre=tmp;
tmp+=strlen(tmp)+1;
PtCHANSON->chemin=tmp;
tmp+=strlen(tmp)+1;
PtCHANSON->longueur=*tmp;
delete []buffer;
ListeChanson->Add(PtCHANSON);
}
FileClose(fic);
}
}
//---------------------------------------------------------------------------
long __fastcall TForm1::ComputeSize(CHANSON & Value)
{
long RetVal=0;
RetVal+=Value.titre.Length()+1;
RetVal+=Value.chemin.Length()+1;
RetVal+=sizeof(Value.longueur);
return RetVal;
} |