Bonjour,

Le code ci-dessous, donné comme exemple dans l'aide Delphi 6, plante sur la ligne [i]StringGrid1.Cells[1,i+1] := Buffer; avec un Access Violation. Je n'arrive pas à déterminer pourquoi. On dirait que la zone mémoire alouée n'est pas remplie au moment de l'exécution de FileRead. Si vous avez une idée...

Pour les besoins du test il faut :

1) Se créer un fichier texte contenant quelques mots
2) un TForm
3) Sur Form1, ajouter un bouton, un StringGrid et un composant TOpenDialog
4) Ajouter le code qui suit sur l'évènement OnClick du Bouton

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.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);
 
      for i := 0 to iBytesRead-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;
Merci

JJE