IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Composants VCL Delphi Discussion :

Savedialog: comment s'en servir pour enregistrer un tableau ?


Sujet :

Composants VCL Delphi

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Âge : 34
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 8
    Points : 4
    Points
    4
    Par défaut Savedialog: comment s'en servir pour enregistrer un tableau ?
    Voila je poste ici et j'espere que c'est au bon endroit en quete d'aide pour faire la chose suivante :
    J'aimerais enregistrer un tableau 100*100 dans un fichier pour pouvoir le récupérer et le réutiliser lors d'une autre session!

    Les déclarations sont les suivantes:

    type
    reg = record
    ec: real;
    e1: real;
    ex: real;
    end;

    var
    table: array [1..300,1..300] of reg;
    J'ai du mal a comprendre comment utiliser le savedialog ..

    J'aimerais en effet pouvoir enregistrer les 100*100 premières cases de mon tableau 300*300 (en le copiant dans un nouveau tableau et avec seulement l'élément .ec du record reg)

    La n'est pas le soucis, jarrive alors avec un tableau 100*100 de réels !
    Comment avec un petit clic sur un bouton, puis-je l'enregistrer dans un fichier avec le savedialog?


    Voila merci à tous ceux qui prendront le temps de me repondre

  2. #2
    Invité1
    Invité(e)
    Par défaut un exemple
    salut
    voici un exemple que j'espere t'aidera je l'ai tiré du help de delphi

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    procedureTForm1.Button1Click(Sender:TObject);
    var
    FileHandle:Integer;
    StringLen:Integer;
    X:Integer;
    Y:Integer;
    begin
     
     ifSaveDialog1.Execute then
     begin
      FileHandle :=FileCreate(SaveDialog1.FileName);
      {Ecrit le nombre de lignes et de colonnes de la grille.}
      FileWrite(FileHandle,
      StringGrid1.ColCount,SizeOf(StringGrid1.ColCount));
      FileWrite(FileHandle,
      StringGrid1.RowCount,SizeOf(StringGrid1.RowCount));
     
     
      forX :=0 toStringGrid1.ColCount –1 do
     
      begin
       forY :=0 toStringGrid1.RowCount –1 do
       begin
        {Ecrit la longueur de chaque chaîne,suivie de la chaîne elle-même.}
        StringLen :=Length(StringGrid1.Cells[X,Y]);
        FileWrite(FileHandle,StringLen,SizeOf(StringLen));
        FileWrite(FileHandle,
        StringGrid1.Cells[X,Y],Length(StringGrid1.Cells[X,Y]);
       end;
      end;
      FileClose(FileHandle);
     
     end;
    end;
    bonne chance
    a++++++++++++

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Âge : 34
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    Faut que je regarde ca de plus pret ^^
    Par contre l'ajout d'un élément stringgrid ne m'emballe pas plus que ca
    Mais bon cest pas dur a utilisé et si apres ca fonctionne pourquoi pas !

    Merci

    EDIT: faudra encore que je vois comment gérer l'ouverture du fichier .. ca doit être la procedure inverse !


    Bon voila j'ai modifié ton code pour arriver a mon resultat :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    procedure TForm1.Button2Click(Sender: TObject);
    var
    FileHandle:Integer;
    StringLen:Integer;
    i:Integer;
    j:Integer;
    t:string;
    begin
    for i:=1 to 100 do
    for j:=1 to 100 do
     Form1.StringGrid1.Cells[i,j]:=FloatToStr(table[i,j].ec);
     if Form1.SaveDialog1.Execute then
     begin
      FileHandle :=FileCreate(SaveDialog1.FileName);
      FileWrite(FileHandle, Form1.StringGrid1.ColCount,SizeOf(100));
      FileWrite(FileHandle, Form1.StringGrid1.RowCount,SizeOf(100));
      for i:=0 to Form1.StringGrid1.ColCount do
      begin
       for j:=0 to Form1.StringGrid1.RowCount do
       begin
        StringLen :=Length(StringGrid1.Cells[i,j]);
        FileWrite(FileHandle, StringLen, SizeOf(StringLen));
        t:=Form1.StringGrid1.Cells[i,j];
        FileWrite(FileHandle, t, Length(Form1.StringGrid1.Cells[i,j]));
       end;
      end;
      FileClose(FileHandle);
     end;
    end;

    Ca enregistre donc bel et bien un fichier (environ 80ko pour le tableau 100*100 de réels) mais il ne contient que des caracteres incompréhensibles
    Je vais essayer de me debrouiller pour l'importer a nouveau merci



    J'avoue avoir du mal a rouvrir le fichier donc je ne refuse aucune aide ^^ lol

  4. #4
    Invité1
    Invité(e)
    Par défaut exemple pour ouvrir
    salut,
    comme pour sauvegarder voici un autre exmple pour ouvrir un fichier, tu sauras l'adapter à ton programme, tu pouras trouver cet exemple dans le help de delphi

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    procedureTForm1.Button1Click(Sender:TObject);
     
    var
    iFileHandle:Integer;
    iFileLength:Integer;
    iBytesRead:Integer;
    Buffer:PChar;
    i:Integer
    begin
    if OpenDialog1.Execute then
    begin
    try
    iFileHandle :=FileOpen(OpenDialog1.FileName,fmOpenRead);
    iFileLength :=FileSeek(iFileHandle,0,2);
    FileSeek(iFileHandle,0,0);
    Buffer :=PChar(AllocMem(iFileLength +1));
    iBytesRead :=FileRead(iFileHandle,Buffer^,iFileLength);
     
    FileClose(iFileHandle);
    fori :=0 toiBytesRead-1 do
    begin
    StringGrid1.RowCount :=StringGrid1.RowCount +1;
    StringGrid1.Cells[1,i+1]:=Buffer[i];
    StringGrid1.Cells[2,i+1]:=IntToStr(Integer(Buffer[i]));
    end;
    finally
    FreeMem(Buffer);
    end;
    end;
    end;
    bonne chance

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Âge : 34
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    Oui j'ai trouvé ce document ^^ et j'ai tenté de l'adapter mais en vain !
    Il faudra que je regarde cela de plus près! Pour l'instant mes essais pour ouvrir le fichier n'ont fini qu'en d'innombrables problèmes! lol
    Merci et bonne soirée !

  6. #6
    Membre expérimenté

    Homme Profil pro
    Inscrit en
    Mars 2004
    Messages
    897
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Marne (Champagne Ardenne)

    Informations professionnelles :
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2004
    Messages : 897
    Points : 1 561
    Points
    1 561
    Par défaut
    Sans passer par une stringGrid, tu peux tout simplement construire tes procédures de sauvegarde et de restauration de ton tableau comme ceci.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    type
      reg = record
        ec: real;
        e1: real;
        ex: real;
      end;
     
      TmyArray = array[1..300,1..300] of reg;
     
      procedure SaveMyArray(AFileName: string; AMyArray: TMyArray);
      procedure LoadMyArray(AFileName: string; var AMyArray: TMyArray);
     
    var
      table: TMyArray;
     
    implementation
     
    procedure SaveMyArray(AFileName: string; AMyArray: TMyArray);
    var
      F: file;
      I,J: integer;
      buff: reg;
      nEcrits: integer;
    begin
      AssignFile(F,AFileName);
      Rewrite(F,1);
      for I := 1 to 300 do
        for J := 1 to 300 do
        begin
          buff := AMyArray[I,J];
          BlockWrite(F,buff,SizeOf(buff),nEcrits);
        end;
      CloseFile(F);
    end;
     
    procedure LoadMyArray(AFileName: string; var AMyArray: TMyArray);
    var
      F: file;
      I,J: integer;
      buff: reg;
      nLus: integer;
    begin
      AssignFile(F,AFileName);
      Reset(F,1);
      for I := 1 to 300 do
        for J := 1 to 300 do
        begin
          BlockRead(F,buff,SizeOf(buff),nLus);
          AMyArray[I,J] := buff;
        end;
      Close(F);
    end;
    pour sauver ton tableau avec un saveDialog

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    if SaveDialog1.Execute then
        SaveMyArray(SaveDialog1.FileName,table);
    Pour relire ton tableau à partir du fichier

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    if OpenDialog1.Execute then
        if FileExists(OpenDialog1.FileName) then
          LoadMyArray(OpenDialog1.FileName,table);
    Attention, il faudra augmenter la taille de la pile dans les options de delphi, car ce tableau 300*300 c'est un peu gros. Taille de la pile $00900000

    Tu peux également utiliser les flux, solution plus interessante à mon gout.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    procedure SaveMyArrayStream(AFileName: string; AMyArray: TMyArray);
    var
      I,J: integer;
      Stream: TStream;
    begin
      Stream := TFileStream.Create(AFileName,fmCreate);
      try
        for I := 1 to 300 do
          for J := 1 to 300 do
            Stream.Write(AMyArray[I,J], SizeOf(AMyArray[I,J]));
      finally
        Stream.Free;
      end;
    end;
     
    procedure LoadMyArrayStream(AFileName: string; var AMyArray: TMyArray);
    var
      I,J: integer;
      Stream: TStream;
    begin
      Stream := TFileStream.Create(AFileName,fmOpenRead);
      try
        for I := 1 to 300 do
          for J := 1 to 300 do
            Stream.Read(AMyArray[I,J], SizeOf(AMyArray[I,J]));
      finally
        Stream.Free;
      end;
    end;
    Pensez à utiliser les tags dans le titre.
    Avant de poser une question reportez-vous à la FAQ Delphi
    Respectez les règles du forum.

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Septembre 2008
    Messages
    8
    Détails du profil
    Informations personnelles :
    Âge : 34
    Localisation : France

    Informations forums :
    Inscription : Septembre 2008
    Messages : 8
    Points : 4
    Points
    4
    Par défaut
    Wouw ! Merci mille fois !
    Grace a ton code j'ai pu réaliser exactement ce que je voulais après une petite adaptation!

    Nouvelles déclarations:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    type
          reg = record
           ec: real; 
           e1: real;
           ex: real;
                end;
     
          TmyArray = array[1..300,1..300] of reg;
          LArray = array[1..100,1..100] of integer;
     
      procedure SaveMyArray(AFileName: string; ALArray: LArray);
      procedure LoadMyArray(AFileName: string; var ALArray: LArray);
     
    var
      table: TmyArray;

    Et les nouvelles procédures :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    procedure SaveMyArray(AFileName: string; ALArray: LArray);
    var
      i,j: integer;
      Stream: TStream;
    begin
      Stream := TFileStream.Create(AFileName,fmCreate);
      try
        for i:=1 to 100 do
          for j:=1 to 100 do
            Stream.Write(ALArray[i,j], SizeOf(ALArray[i,j]));
      finally
        Stream.Free;
      end;
    end;
     
    procedure LoadMyArray(AFileName: string; var ALArray: LArray);
    var
      i,j: integer;
      Stream: TStream;
    begin
      Stream := TFileStream.Create(AFileName,fmOpenRead);
      try
        for i:=1 to 100 do
          for j:=1 to 100 do
            Stream.Read(ALArray[i,j], SizeOf(ALArray[i,j]));
      finally
        Stream.Free;
      end;
    end;
     
     
    procedure tabletosave(var tablesave:LArray);
    var i,j:integer;
    begin
    for i:=1 to 100 do
    for j:=1 to 100 do
    begin
    if ((table[i,j].ec=-2) or (table[i,j].ec=-1)) then tablesave[i,j]:=trunc(table[i,j].ec);
    if (table[i,j].ec=f1) then tablesave[i,j]:=1;
    if (table[i,j].ec=f2) then tablesave[i,j]:=2;
    if (table[i,j].ec=f3) then tablesave[i,j]:=3;
    end;
    end;
     
    procedure TForm1.Button2Click(Sender: TObject);
    var tablesave: LArray;
    begin
    tabletosave(tablesave);
    if SaveDialog1.Execute then
        SaveMyArray(SaveDialog1.FileName,tablesave);
    end;
     
    procedure tabletoload(tablesave:LArray);
    var i,j:integer;
    begin
    for i:=1 to 100 do
    for j:=1 to 100 do
    begin
    if ((tablesave[i,j]=-1) or (tablesave[i,j]=-2)) then table[i,j].ec:=tablesave[i,j];
    if tablesave[i,j]=1 then table[i,j].ec:=f1;
    if tablesave[i,j]=2 then table[i,j].ec:=f2;
    if tablesave[i,j]=3 then table[i,j].ec:=f3;
    end;
    end;
     
    procedure TForm1.Button11Click(Sender: TObject);
    var tablesave: LArray;
    begin
    if OpenDialog1.Execute then
        if FileExists(OpenDialog1.FileName) then
          LoadMyArray(OpenDialog1.FileName,tablesave);
    tabletoload(tablesave);
    colorer;
    end;
    Bon cela a été adapté à mes besoins mais le code est bon


    Merci beaucoup

  8. #8
    Invité1
    Invité(e)
    Par défaut salut
    regarde cette solution

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    implementation
     Type
      TElement=Packed Record
         Donne : array[1..100] of String[5];
       End;
     
    var fichier : file of Telement;
        element : Telement;    
    {$R *.dfm}
     
     
    procedure TForm1.Button1Click(Sender: TObject);
    var i , j : integer;
    begin
    if savedialog1.Execute  then
    begin
     AssignFile(fichier,savedialog1.FileName);
     Rewrite(fichier);
    for i := 1 to 100 do
     begin
      for j := 1 to 100 do
       element.Donne[j] := '50';
       Write(fichier,Element);
     end;
     CloseFile(fichier);
    end;
    end;
     
     
    procedure TForm1.Button2Click(Sender: TObject);
    var i , j : integer;
    begin
     i := 1;
     if opendialog1.Execute then
     begin
     AssignFile(fichier,opendialog1.FileName);
     reset(fichier);
     While Not Eof(Fichier) Do
      Begin
       Read(Fichier,Element);
       for j := 1 to 100 do
        StringGrid1.Cells[i,j] := Element.donne[j];
        i := i + 1;
       end;
    end;
    bonne chance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XL-2007] Comment créée une maccro pour enregistrer un document ".MAC" ?
    Par matthieu27220 dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 15/10/2014, 16h45
  2. Réponses: 2
    Dernier message: 24/04/2012, 14h22
  3. Réponses: 5
    Dernier message: 07/09/2011, 15h12
  4. Comment réaliser une boucle pour remplir un tableau
    Par LVChatel dans le forum Général JavaScript
    Réponses: 0
    Dernier message: 03/04/2009, 12h20
  5. [PowerAMC] Comment s'en servir pour creer une base?
    Par Elmilouse dans le forum Access
    Réponses: 2
    Dernier message: 27/07/2004, 10h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo