Filestream readbuffer

cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007 - 18 déc. 2005 à 19:19
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 - 19 déc. 2005 à 16:19
est-ce que quelqun peut me donner la procédure inverse du code (c'est a dire pour lire a partir du fichier)

procedure TForm1.saveExecute(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
begin
{ Execution de la boite de dialogue.
C'est une TSaveDialog dont la propriété DefaultExt contient l'extension voulue. }
if not ecriture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(ecriture.FileName,fmCreate);
try
{ Ecriture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.WriteBuffer(form2.StringGrid1.ColCount,SizeOf(Integer));
Fs.WriteBuffer(form2.StringGrid1.RowCount,SizeOf(Integer));


{ Boucle pour enregistrer toutes les chaines. }
for X := 0 to form2.StringGrid1.ColCount -1 do
for Y := 0 to form2.StringGrid1.RowCount -1 do
begin
{ Récupération de la talle de la chaine. }
L := Length(form2.StringGrid1.Cells[X,Y]);
{ Ecriture de la taille de la chaine. }
Fs.WriteBuffer(L,SizeOf(Integer));
{ Puis écriture de la chaine elle-même. }
Fs.WriteBuffer(form2.StringGrid1.Cells[X,Y][1],L);
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;

merci
bye

7 réponses

cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
18 déc. 2005 à 19:23
petite correction , lorsque je remplace writebuffer par readbuffer il me met:
un objet constante ne peut être passé comme paramètre var.
merci bye
0
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
18 déc. 2005 à 22:20
Salut
Esaye ceci:

procedure TForm1.Button2Click(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
Buff : Array[0..255] of char;
begin
{ Execution de la boite de dialogue.
C'est une TOpenDialog dont la propriété DefaultExt contient l'extension voulue.
Avec FileMustExist à True}
if not Lecture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(Lecture.FileName,fmOpenRead );
try
{ Lecture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.ReadBuffer(L,SizeOf(Integer));
StringGrid1.ColCount := L;
Fs.ReadBuffer(L,SizeOf(Integer));
StringGrid1.RowCount := L;


{ Boucle pour Lire toutes les chaines. }
for X := 0 to StringGrid1.ColCount -1 do
for Y := 0 to StringGrid1.RowCount-1 do
begin
{ Rempli Buffer avec des #0 }
FillChar(Buff,Length(Buff),#0);
{ Lecture de la taille de la chaine. }
Fs.ReadBuffer(L,SizeOf(Integer));
{ Puis Lecture de la chaine elle-même. }
Fs.ReadBuffer(Buff,L); StringGrid1.Cells[X,Y]:= Buff;
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;



@+

Cirec
0
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
19 déc. 2005 à 11:37
merci cirec je n'ai eu aucune erreure mais lorsque jenregistre le fichier il me met erreur d'ecriture dans le flux. est-ce que tu pe m'aider stp
merci
bye

ps: je suis débutant
0
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
19 déc. 2005 à 12:42
Je ne sais pas si j'ai bien compris, mais tu dis que quand tu enregistres le fichier (avec ta procédure je suppose ?) il te met une erreur d'écriture dans le flux. Si c'est ça j'avoue que c'est un peut bizarre puisque j'ai moi même testé les deux conjointement et je n'ai eu aucune erreur.

Pourrais-tu être un peut plus précis quand à l'origine de l'erreur (à quel niveau du code elle se produit où à quel instant)


@+
Cirec
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
19 déc. 2005 à 16:00
voila je vais être plus clair, lors de la compilation il n'y a aucune erreure mais lorsque je veux ouvrir un fichier au début(formcreate) j'ai 'EREADERROR' erreur de lecture dans le flux et quand j'essaie d'enregistrer a la fin (formdestroy) j'ai 'ewriteerror' erreur d'ecriture dans le flux.
voici le source

unit Unit1;


interface


uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,inifiles, WinSkinData, ActnList;


type
TForm1 = class(TForm)
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
LabeledEdit3: TLabeledEdit;
ComboBox1: TComboBox;
Label1: TLabel;
BitBtn1: TBitBtn;
Label2: TLabel;
BitBtn2: TBitBtn;
ecriture: TSaveDialog;
lecture: TOpenDialog;
SkinData1: TSkinData;
ActionList1: TActionList;
save: TAction;
load: TAction;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure loadExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);



private
{ Déclarations privées }
public
{ Déclarations publiques }


end;


var
Form1: TForm1;


implementation
uses
Unit2;
{$R *.dfm}


procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form2.show;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
ligne:integer;
begin
if (form2.StringGrid1.RowCount=1) and (form2.stringgrid1.cells[0,0]='') then
begin
Form2.StringGrid1.Cells[0,0]:=labeledEdit1.Text;
Form2.StringGrid1.Cells[1,0]:=labeledEdit2.text;
Form2.StringGrid1.Cells[2,0]:=labeledEdit3.text;
Form2.StringGrid1.Cells[3,0]:=combobox1.text;
end
else
begin
ligne:=form2.stringgrid1.RowCount-1;
Form2.StringGrid1.Cells[0,ligne]:=labeledEdit1.Text;
Form2.StringGrid1.Cells[1,ligne]:=labeledEdit2.text;
Form2.StringGrid1.Cells[2,ligne]:=labeledEdit3.text;
Form2.StringGrid1.Cells[3,ligne]:=combobox1.text;
Form2.stringgrid1.rowcount:=Form2.stringgrid1.rowcount+1;
form2.sbrefresh(self);
end;
end;



procedure TForm1.FormDestroy(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
begin
{ Execution de la boite de dialogue.
C'est une TSaveDialog dont la propriété DefaultExt contient l'extension voulue. }
if not ecriture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(ecriture.FileName,fmCreate);
try
{ Ecriture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.WriteBuffer(form2.StringGrid1.ColCount,SizeOf(Integer));
Fs.WriteBuffer(form2.StringGrid1.RowCount,SizeOf(Integer));


{ Boucle pour enregistrer toutes les chaines. }
for X := 0 to form2.StringGrid1.ColCount -1 do
for Y := 0 to form2.StringGrid1.RowCount -1 do
begin
{ Récupération de la talle de la chaine. }
L := Length(form2.StringGrid1.Cells[X,Y]);
{ Ecriture de la taille de la chaine. }
Fs.WriteBuffer(L,SizeOf(Integer));
{ Puis écriture de la chaine elle-même. }
Fs.WriteBuffer(form2.StringGrid1.Cells[X,Y][1],L);
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;


procedure TForm1.loadExecute(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
Buff : Array[0..255] of char;
begin
{ Execution de la boite de dialogue.
C'est une TOpenDialog dont la propriété DefaultExt contient l'extension voulue.
Avec FileMustExist à True}
if not Lecture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(Lecture.FileName,fmOpenRead );
try
{ Lecture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.ReadBuffer(L,SizeOf(Integer));
Form2.StringGrid1.ColCount := L;
Fs.ReadBuffer(L,SizeOf(Integer));
Form2.StringGrid1.RowCount := L;


{ Boucle pour Lire toutes les chaines. }
for X := 0 to Form2.StringGrid1.ColCount -1 do
for Y := 0 to Form2.StringGrid1.RowCount-1 do
begin
{ Rempli Buffer avec des #0 }
FillChar(Buff,Length(Buff),#0);
{ Lecture de la taille de la chaine. }
Fs.ReadBuffer(L,SizeOf(Integer));
{ Puis Lecture de la chaine elle-même. }
Fs.ReadBuffer(Buff,L); Form2.StringGrid1.Cells[X,Y]:= Buff;
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
lecture.InitialDir:= ExtractFilePath((Application.ExeName)+'\liste');
ecriture.InitialDir:= ExtractFilePath((Application.ExeName)+'\liste');
loadexecute(self);
end;


end.

bye
0
cs_saravana Messages postés 91 Date d'inscription vendredi 25 mars 2005 Statut Membre Dernière intervention 18 octobre 2007
19 déc. 2005 à 16:01
voila je vais être plus clair, lors de la compilation il n'y a aucune erreure mais lorsque je veux ouvrir un fichier au début(formcreate) j'ai 'EREADERROR' erreur de lecture dans le flux et quand j'essaie d'enregistrer a la fin (formdestroy) j'ai 'ewriteerror' erreur d'ecriture dans le flux.
voici le source

unit Unit1;


interface


uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls,inifiles, WinSkinData, ActnList;


type
TForm1 = class(TForm)
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
LabeledEdit3: TLabeledEdit;
ComboBox1: TComboBox;
Label1: TLabel;
BitBtn1: TBitBtn;
Label2: TLabel;
BitBtn2: TBitBtn;
ecriture: TSaveDialog;
lecture: TOpenDialog;
SkinData1: TSkinData;
ActionList1: TActionList;
save: TAction;
load: TAction;
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure loadExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);



private
{ Déclarations privées }
public
{ Déclarations publiques }


end;


var
Form1: TForm1;


implementation
uses
Unit2;
{$R *.dfm}


procedure TForm1.BitBtn2Click(Sender: TObject);
begin
Form2.show;
end;


procedure TForm1.BitBtn1Click(Sender: TObject);
var
ligne:integer;
begin
if (form2.StringGrid1.RowCount=1) and (form2.stringgrid1.cells[0,0]='') then
begin
Form2.StringGrid1.Cells[0,0]:=labeledEdit1.Text;
Form2.StringGrid1.Cells[1,0]:=labeledEdit2.text;
Form2.StringGrid1.Cells[2,0]:=labeledEdit3.text;
Form2.StringGrid1.Cells[3,0]:=combobox1.text;
end
else
begin
ligne:=form2.stringgrid1.RowCount-1;
Form2.StringGrid1.Cells[0,ligne]:=labeledEdit1.Text;
Form2.StringGrid1.Cells[1,ligne]:=labeledEdit2.text;
Form2.StringGrid1.Cells[2,ligne]:=labeledEdit3.text;
Form2.StringGrid1.Cells[3,ligne]:=combobox1.text;
Form2.stringgrid1.rowcount:=Form2.stringgrid1.rowcount+1;
form2.sbrefresh(self);
end;
end;



procedure TForm1.FormDestroy(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
begin
{ Execution de la boite de dialogue.
C'est une TSaveDialog dont la propriété DefaultExt contient l'extension voulue. }
if not ecriture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(ecriture.FileName,fmCreate);
try
{ Ecriture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.WriteBuffer(form2.StringGrid1.ColCount,SizeOf(Integer));
Fs.WriteBuffer(form2.StringGrid1.RowCount,SizeOf(Integer));


{ Boucle pour enregistrer toutes les chaines. }
for X := 0 to form2.StringGrid1.ColCount -1 do
for Y := 0 to form2.StringGrid1.RowCount -1 do
begin
{ Récupération de la talle de la chaine. }
L := Length(form2.StringGrid1.Cells[X,Y]);
{ Ecriture de la taille de la chaine. }
Fs.WriteBuffer(L,SizeOf(Integer));
{ Puis écriture de la chaine elle-même. }
Fs.WriteBuffer(form2.StringGrid1.Cells[X,Y][1],L);
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;


procedure TForm1.loadExecute(Sender: TObject);
var
X, Y, L: Integer;
Fs: TFileStream;
Buff : Array[0..255] of char;
begin
{ Execution de la boite de dialogue.
C'est une TOpenDialog dont la propriété DefaultExt contient l'extension voulue.
Avec FileMustExist à True}
if not Lecture.Execute then
Exit;


{ Création du'un flux fichier. }
Fs := TFileStream.Create(Lecture.FileName,fmOpenRead );
try
{ Lecture de la taille du StringGrid (ici, il s'appelle sg). }
Fs.ReadBuffer(L,SizeOf(Integer));
Form2.StringGrid1.ColCount := L;
Fs.ReadBuffer(L,SizeOf(Integer));
Form2.StringGrid1.RowCount := L;


{ Boucle pour Lire toutes les chaines. }
for X := 0 to Form2.StringGrid1.ColCount -1 do
for Y := 0 to Form2.StringGrid1.RowCount-1 do
begin
{ Rempli Buffer avec des #0 }
FillChar(Buff,Length(Buff),#0);
{ Lecture de la taille de la chaine. }
Fs.ReadBuffer(L,SizeOf(Integer));
{ Puis Lecture de la chaine elle-même. }
Fs.ReadBuffer(Buff,L); Form2.StringGrid1.Cells[X,Y]:= Buff;
end;


finally
{ Libération du flux. }
Fs.Free;
end; // try.
end;



procedure TForm1.FormCreate(Sender: TObject);
begin

loadexecute(self);
end;


end.

bye
0
Cirec Messages postés 3833 Date d'inscription vendredi 23 juillet 2004 Statut Modérateur Dernière intervention 18 septembre 2022 50
19 déc. 2005 à 16:19
essaye déjà de mettre la lecture non pas dans OnCreate mais dans le OnShow et l'écriture dans le OnClose de ta Form



@+
Cirec
0
Rejoignez-nous