【问题标题】:Using Delimiter when reading lines from a textfile in Delphi在 Delphi 中从文本文件中读取行时使用分隔符
【发布时间】:2014-05-05 01:01:10
【问题描述】:

我遇到了一个我不知道如何真正解决的问题。目前我正在从一个文本文件中读取几行,但是这些行中有一些我不想保留的值。线条就像

int String Float float 浮点数

我只想拥有字符串部分并将该名称分配给列表框。我尝试只为空格使用分隔符,但我意识到它并没有重置到下一行的开头,而是从最后一个地方继续。

文本文件的第一行是

1 勿洞 1.7 .24 2300

2 阿尔梅拉德勿洞 1.28 .26 2100

3 水泥布鲁克 .93 .29 1800

使用带空格的分隔符给我

1

军械

.93

虽然我希望它至少是

1

2

3

我现在的代码是一个按钮的 onClickEvent 过程

  var
   SomeTxtFile : TextFile;
   buffer : string;
   holder : TStringList;
   idx:integer;
 begin
 idx:=-1;
  holder:=TStringList.Create;
   AssignFile(SomeTxtFile, 'Opaque.lib') ;
   Reset(SomeTxtFile) ;
   while not EOF(SomeTxtFile) do
   begin
   idx:=idx+1;
    ReadLn(SomeTxtFile, buffer) ;
    holder.Delimiter:=' ';
    holder.DelimitedText:=buffer;
    ShowMessage(buffer) ;
    WallsListBox.Items.Add(holder[idx]);
   end;
   CloseFile(SomeTxtFile) ;

我在想也许我可以用很多标志来做我想做的事,这些标志试图将字符串的一部分转换为浮点/整数,但这似乎很愚蠢。有什么建议吗?

谢谢!

【问题讨论】:

  • 您添加到列表框持有人[idx]。并且 idx 在您阅读的每一行都会增加。您应该将 holder[0] 添加到列表框。
  • 如果你想帮自己一个忙,扔掉所有的 TextFile 例程,改用流。每当有人使用 AssignFile() 时,一只小猫就会死去。
  • @JensG 我不知道如何使用流,所以很抱歉,但我想我必须成为一个小猫杀手:/
  • 您使用的是哪个版本的 Delphi?

标签: delphi


【解决方案1】:

您正在打印列表的第 idx 个元素。要打印第一个元素,请使用

WallsListBox.Items.Add(holder[0]);

打印第二个(字符串)部分 - 使用

WallsListBox.Items.Add(holder[1]);

等等。请注意,字符串部分可能包含一些单词(Armerad Betong),因此您需要分析 holder 内容。示例:

var
  TextList, Holder: TStringList;
  s: string;
  i, j: integer;
  dummy: Double;
begin
  TextList := TStringList.Create;
  Holder := TStringList.Create;
  TextList.LoadFromFile('Opaque.lib');
  for i := 0 to TextList.Count - 1 do begin
    Holder.CommaText := TextList[i];
    if Holder.Count >= 2 then begin
      s := Holder[1];
      j := 2;
      while (j < Holder.Count) and
        (not TryStrToFloat(Holder[j], dummy)) do begin
        s := s + ' ' + Holder[j];
        Inc(j);
      end;
      Memo1.Lines.Add(s);
    end;
  end;
  Holder.Free;
  TextList.Free;

【讨论】:

  • 哦,现在我明白了!谢谢!通过分析持有人的内容,我需要做一些标志可能对吗?
【解决方案2】:

我将使用另一种方法:删除最后三个单词和第一个单词,而不是使用 TStringList 来“分隔”文本(当您要提取的字符串中有空格时它不起作用) (空格分隔)从字符串。剩下的必须是您所追求的文本(假设所有行都包含格式 int String Float float float):

var
   SomeTxtFile : TextFile;
   buffer : string;
   idx:integer;

begin
  AssignFile(SomeTxtFile, 'Opaque.lib') ;
  Reset(SomeTxtFile) ;
  while not EOF(SomeTxtFile) do begin
    ReadLn(SomeTxtFile, buffer);
    ShowMessage(buffer);
    idx:=length(buffer);
    // First eliminitate all trailing spaces
    while buffer[idx]=' ' do dec(idx);
    // Then eliminate the third float value (search backwards for the space seperator)
    while buffer[idx]<>' ' do dec(idx);
    // Then eliminitate all spaces before the third float value
    while buffer[idx]=' ' do dec(idx);
    // Then eliminate the second float value (search backwards for the space seperator)
    while buffer[idx]<>' ' do dec(idx);
    // Then eliminitate all spaces before the second float value
    while buffer[idx]=' ' do dec(idx);
    // Then eliminate the first float value (search backwards for the space seperator)
    while buffer[idx]<>' ' do dec(idx);
    // Then eliminitate all spaces before the first float value
    while buffer[idx]=' ' do dec(idx);
    // Truncate the string to elimate all the characters we have determined to be the float values and their seperating spaces
    setlength(buffer,idx);
    // Now search from the beginning of the string
    idx:=1;
    // First eliminate all leading spaces
    while buffer[idx]=' ' do inc(idx);
    // Then eliminate the integer value
    while buffer[idx]<>' ' do inc(idx);
    // Then eliminate the spaces following the integer value
    while buffer[idx]=' ' do inc(idx);
    // We are now at the first character of the string we want, so step back one character
    dec(idx);
    // Delete the characters from the string that we have determined to be the integer value and the spaces surrounding it
    delete(buffer,1,idx);
    WallsListBox.Items.Add(buffer);
  end;
  CloseFile(SomeTxtFile);
end.

您也可以使用内置的 POS 函数,但我相信上面的代码对于仅具有 Delphi/PASCAL 语言基本知识的代码(看起来没有冒犯)更容易理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多