Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 从文本文件加载StringGrid_Delphi_Load_Numbers_Tstringgrid - Fatal编程技术网

Delphi 从文本文件加载StringGrid

Delphi 从文本文件加载StringGrid,delphi,load,numbers,tstringgrid,Delphi,Load,Numbers,Tstringgrid,如何将整数以空格分隔的文本文件加载到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;

如何将整数以空格分隔的文本文件加载到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;

我建议尝试一下这个代码。
这是基本的,但我相信只要稍加改变,你就能解决你的问题

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);

Beny

我个人会选择在这里不使用Pascal IO。如果您希望代码能够读取Unicode数据,则Pascal IO无法帮助您

您可以使用字符串列表加载文件,然后从
StrUtils
单元中
SplitString
来解析字符串

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;
两个单词之间有4个空格,
SplitString
将返回以下数组:

'Hello'
''
''
''
'World'
如果希望将连续分隔符视为一个分隔符,则可以使用字符串列表的
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;
过程PopulateStringGrid(网格:TStringGrid;常量文件名:string);
变量
文本文件,行:TStringList;
行:整数;
开始
Grid.RowCount:=0//清除任何以前的数据
TextFile:=TStringList.Create;
尝试
行:=TStringList.Create;
尝试
行分隔符:='';
TextFile.LoadFromFile(文件名);
Grid.RowCount:=TextFile.Count;
对于行:=0到TextFile.Count-1 do
开始
Line.DelimitedText:=TextFile[Row];
对于Col:=0到Grid.ColCount-1 do
如果Col试试这个

    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;
procedure-FillStringgrid;
//将文本行拆分为单个条目
过程拆分(常量分隔符:Char;输入:string;常量字符串:TStrings);
开始
断言(赋值(字符串));
字符串。清晰;
Strings.Delimiter:=分隔符;
Strings.DelimitedText:=输入;
结束;
变量
strlst:Tstringlist;
myfile:TextFile;
搜索:字符串;
i、 j:整数;
开始
i:=0;
AssignFile(myfile,'filepath');//在此处指定文件路径
重置(myFile);
而不是eof(myfile)做什么
开始
Readln(myfile,search);
strlst:=Tstringlist.Create;
拆分(“”,搜索,strlst);//获取由分隔符分隔的编号
//根据条目数调整列计数
如果StringGrid1.ColCount
为什么要使用Pascal IO?为什么不将文件加载到一个字符串列表中,并使用该函数分割每一行呢?忽略任何PASCAL I/O组,这对该任务来说都是很好的。您的问题陈述不一致,如果缺少多个连续数字,您将如何确定位置?@user539484纯粹是恶意和怨恨。不雅观。
    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;