0

スペースで区切られた整数のテキスト ファイルを StringGrid にロードするにはどうすればよいですか? 各セルに各番号。グリッドは四角形でなければならないので、欠落している数字がある場合は 0 で埋める必要があります。

これまでに行ったことは次のとおりですが、行と列の数を既に設定している必要があります。

  while not eof(f) do
  begin
    while not eoln(f) do
    begin
      read(f, data);
      StringGrid1.Cells[p, l] := data;
      inc(p);
    end;
    p := 0;
    readln(f);
    inc(l);
  end;
4

3 に答える 3

1

個人的には、ここでは Pascal IO を使用しないことにします。コードで Unicode データを読み取れるようにしたい場合、Pascal IO は役に立ちません。

文字列リストを使用してファイルをロードSplitStringし、StrUtilsユニットから文字列を解析することで、説明したことを実行できます。

procedure PopulateStringGrid(Grid: TStringGrid; const FileName: string);
var
  Strings: TStringList;
  Row, Col: Integer;
  Items: TStringDynArray;
begin
  Grid.RowCount := 0;//clear any previous data
  Strings := TStringList.Create;
  try
    Strings.LoadFromFile(FileName);
    Grid.RowCount := Strings.Count;
    for Row := 0 to Strings.Count-1 do
    begin
      Items := SplitString(Strings[Row], ' ');
      for Col := 0 to Grid.ColCount-1 do
        if Col<Length(Items) then
          Grid.Cells[Col, Row] := Items[Col]
        else
          Grid.Cells[Col, Row] := '0';
    end;
  finally
    Strings.Free;
  end;
end;

SplitString正確に必要なものではない可能性があることに注意してください。たとえば、繰り返される区切り文字を 1 つに結合しません。私が何を意味するかを理解するには、次の入力を検討してください。

Hello    World

2 つの単語の間には 4 つのスペースがありSplitString、次の配列が返されます。

'Hello'
''
''
''
'World'

連続した区切り文字を 1 つの区切り文字として扱いたい場合DelimitedTextは、文字列リストのプロパティを使用できます。

procedure PopulateStringGrid(Grid: TStringGrid; const FileName: string);
var
  TextFile, Line: TStringList;
  Row: Integer;
begin
  Grid.RowCount := 0;//clear any previous data
  TextFile := TStringList.Create;
  try
    Line := TStringList.Create;
    try
      Line.Delimiter := ' ';
      TextFile.LoadFromFile(FileName);
      Grid.RowCount := TextFile.Count;
      for Row := 0 to TextFile.Count-1 do
      begin
        Line.DelimitedText := TextFile[Row];
        for Col := 0 to Grid.ColCount-1 do
          if Col<Line.Count then
            Grid.Cells[Col, Row] := Line[Col]
          else
            Grid.Cells[Col, Row] := '0';
      end;
    finally
      Line.Free;
    end;
  finally
    TextFile.Free;
  end;
end;
于 2012-02-06T09:47:22.137 に答える
0

これを試して

    procedure FillStringgrid;
        // Split the line of text into individual entries
        procedure Split(const Delimiter: Char;Input: string;const Strings:TStrings);
        begin
            Assert(Assigned(Strings)) ;
            Strings.Clear;
            Strings.Delimiter := Delimiter;
            Strings.DelimitedText := Input;
        end;
    var
      strlst  : Tstringlist;
      myfile  : TextFile;
      search  : string;
      i,j     : integer;
    begin
      i:= 0;
      AssignFile(myfile,'filepath');   // specify your file path here
      Reset(myFile);
      while not eof(myfile) do
      begin
          Readln(myfile,search);
          strlst:= Tstringlist.Create;
          Split(' ',search,strlst);    // get the no's separated by the delimiter  
          //adjust your column count based on no of entries
          if StringGrid1.ColCount < strlst.Count then
             StringGrid1.ColCount := strlst.Count;
          StringGrid1.Rows[i]:=strlst; // adjust the row count
          Inc(i);
          StringGrid1.RowCount := i;
      end;
      // free stringlist and textfile      
      CloseFile(myfile) ;
      strlst .free;

      // fill in the blank entries with 0
      for i := 0 to StringGrid1.RowCount - 1 do
        begin
        for j := 0 to StringGrid1.ColCount - 1 do
          begin
          if StringGrid1.Cells[j,i]='' then
            StringGrid1.Cells[j,i]:='0';
          end;
        end; 
    end;
于 2012-02-06T10:18:55.123 に答える
0

このコードを試してみることをお勧めします。
それは基本的ですが、ひねりを加えれば問題を解決できると確信しています。

procedure LoadFile(FileName: string; StringGrid: TStringGrid);
var
  temp, fName, sName, eMail: string;
  sgItem: TStringList;
  f: textfile;
begin
  assignfile(f, FileName);
  reset(f);
  sgItem := TStringList.Create;
  StringGrid.RowCount := 2;
  while not eof(f) do
  begin
    readln(f, temp);
    fName := copy(temp, 1, pos('|', temp) - 1);
    delete(temp, 1, pos('|', temp));
    sName := copy(temp, 1, pos('|', temp) - 1);
    delete(temp, 1, pos('|', temp));
    eMail := temp;
    sgItem.Clear;
    sgItem.Add(fName);
    sgItem.Add(sName);
    sgItem.Add(eMail);
    StringGrid.Rows[StringGrid.RowCount - 1].AddStrings(sgItem);
    StringGrid.RowCount := StringGrid.RowCount + 1;
  end;
  sgItem.Free;
  closefile(f);
end;

使用法 :

LoadFile('File.txt', StringGrid1);

ベニー

于 2012-02-05T23:59:13.750 に答える