在Delphi 2010中,如何从具有数字和字母的文件中读取行? [英] How do I read a row from a file that has both numbers and letters in Delphi 2010?

查看:74
本文介绍了在Delphi 2010中,如何从具有数字和字母的文件中读取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,该文件在任何给定的行上都有以文本格式和数字格式表示的数据。像这样的东西:

I have a text file that has, on any given row, data that are expressed both in text format and in numeric format. Something like this:

Dog 5 4 7

如何在Delphi中编写文件读取例程,该例程读取该行并将读取的值分配给正确的变量( Dog转换为字符串变量, 5, 4和 7转换为实数或整数变量)?

How do I write a file reading routine in Delphi that reads this row and assigns the read values into the correct variables ("Dog" into a string variable and "5", "4" and "7" into real or integer variables)?

推荐答案

您可以使用 SplitString StrUtils 的$ c>将字符串分割成多个部分。然后使用 StrToInt 转换为整数。

You can use SplitString from StrUtils to split the string into pieces. And then use StrToInt to convert to integer.

uses
  StrUtils;
....
var
  Fields: TStringDynArray;
....
Fields := SplitString(Row, ' ');
StrVar := Fields[0];
IntVar1 := StrToInt(Fields[1]);
IntVar2 := StrToInt(Fields[2]);
IntVar3 := StrToInt(Fields[3]);

如果您有浮动账户,显然可以用 StrToFloat 代替点值。

And obviously substitute StrToFloat if you have floating point values.

这篇关于在Delphi 2010中,如何从具有数字和字母的文件中读取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆