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

Delphi Discussion :

charger un fichier dans un tableau


Sujet :

Delphi

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Octobre 2004
    Messages
    19
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2004
    Messages : 19
    Points : 13
    Points
    13
    Par défaut charger un fichier dans un tableau
    Bonjour,

    Quelle est la syntaxe de la fonction qui me permettrait de charger un fichier dans une StringGrid?

    Mon fic est le suivant:
    44 49 41 10 61 05 06 10 10 00 01 00 86 00 00 01
    00 00 00 00 45 54 41 20 45 53 30 32 20 20 69 05
    09 21 13 31 91 2e ff 96 d6 00 00 0a a7 00 05 31
    74 13 31 51 50 80 00 c8 a0 56 31 20 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 14 47 a6 80 00 5e
    3a 7c 20 45 74 61 74 20 43 68 61 ee 6e 65 a0 3a
    20 27 53 74 61 6e 64 62 79 27 20 20 20 20 20 20
    20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
    20 20 20 20 fa 9c
    Chaque octet (XX) doit être dans une case de la StringGrid.

    Merci

  2. #2
    Membre confirmé
    Avatar de lil_jam63
    Profil pro
    Inscrit en
    Janvier 2004
    Messages
    447
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2004
    Messages : 447
    Points : 600
    Points
    600
    Par défaut
    il n'y a rien qui te permet de charger directement dans un TStringGrid tes informations, je pense que tu devrais traiter ton fichier ligne par ligne.
    Tu charges la ligne dans un TStringList (l'espace agit en séparateur) ensuite avec une boucle sur TSTringList.Count-1 tu charges l'indice i dans le TStringGrid à l'indice colonne i.
    Ensuite vidage du TStringList et c'est repartit.

  3. #3
    Expert confirmé
    Avatar de Sub0
    Homme Profil pro
    Développeur Web
    Inscrit en
    Décembre 2002
    Messages
    3 573
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 51

    Informations professionnelles :
    Activité : Développeur Web

    Informations forums :
    Inscription : Décembre 2002
    Messages : 3 573
    Points : 4 219
    Points
    4 219
    Par défaut
    Salut!

    Perso, j'utiliserais BlockRead, 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
    Uses Math;
     
    Var
      buf: Array[0..$FFFF] Of Byte;
      sz: Integer;
     
    {----------------------------------------------------------------}
    Procedure TForm1.FormActivate(Sender: TObject);
    Var F: File;
    Begin
      StringGrid1.Align := alClient;
      StringGrid1.Font.Name := 'Lucida console';
      StringGrid1.Font.Size := 10;
      StringGrid1.DefaultColWidth := 24;
      StringGrid1.DefaultRowHeight := 16;
      StringGrid1.ColCount := StringGrid1.FixedCols + 16;
      If Not (OpenDialog1.Execute) Then Exit;
      FillChar(buf, SizeOf(buf), 0);
      AssignFile(F, OpenDialog1.FileName);
      Reset(F, 1);
      sz := FileSize(F);
      If (sz > 0) Then BlockRead(F, Buf, sz);
      CloseFile(F);
      StringGrid1.RowCount := Ceil(sz / 16) + StringGrid1.FixedRows;
      StringGrid1.Refresh;
    End;
     
    {----------------------------------------------------------------}
    Procedure TForm1.StringGrid1DrawCell(Sender: TObject;
      ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    Var z: Integer;
    Begin
      If (State = [gdFixed]) Then Exit;
      z := (ACol - 1) + (ARow - 1) * 16;
      If (z >= sz) Then Exit;
      StringGrid1.Canvas.TextRect(Rect,
        Rect.Left + 4, Rect.Top + 3, IntToHex(Buf[z], 2));
    End;
     
    {----------------------------------------------------------------}
    End.

    Lien tout en bas à gauche de cette fenêtre

  4. #4
    Expert éminent sénior
    Avatar de Jipété
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    10 951
    Détails du profil
    Informations personnelles :
    Localisation : France, Hérault (Languedoc Roussillon)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 10 951
    Points : 15 410
    Points
    15 410
    Par défaut
    Salut.
    Je viens de tester la routine à Sub0, merci à lui, mais "Erreur E/S 998" si fichier > 80 ko environ (pas cherché exactement).
    Voilà mes corrections, inspirées de ça (pas très vieux).
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    Var
    //  buf: Array[0..$FFFF] Of Byte;
      Buf: array of Byte;
    et
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
        sz := FileSize(F);
        SetLength(Buf, sz); //   <--------- Insérer cette ligne
        If (sz > 0) Then BlockRead(F, Buf[0], sz); // Erreur E/S 998 si gros
    Testé avec un 4 mo sans pb, w2k D7.

    Mes 2 cts,
    --
    jp

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

Discussions similaires

  1. charger des parties d'un fichier dans un tableau
    Par leila32 dans le forum C++
    Réponses: 14
    Dernier message: 08/03/2013, 13h06
  2. Charger un fichier dans un tableau de structures
    Par ysahel dans le forum Débuter
    Réponses: 7
    Dernier message: 17/01/2010, 13h18
  3. charger un fichier dans un tableau
    Par siempre dans le forum C
    Réponses: 12
    Dernier message: 20/11/2009, 09h15
  4. Charger un fichier dans tableau d'entier
    Par nico0007 dans le forum Langage
    Réponses: 12
    Dernier message: 17/04/2008, 11h30
  5. Réponses: 4
    Dernier message: 10/10/2003, 18h04

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