0

我正在使用 VB6 代码并将其转换为 Delphi。

使用顺序打开文件的 VB6 代码:

Dim bytNumDataPoints As Byte
Dim bytcount as Byte
Dim lintData(0 To 23) As Long
    Input #intFileNumber, bytNumDataPoints
    For bytcount = 0 To (bytNumDataPoints - 1)
        Input #intFileNumber, lintData(bytcount)  '
        lintData(bytcount) = (lintData(bytcount) + 65536) Mod 65536
    Next bytcount

档案资料:

24  <<<<<<<<<<<< Number Data Points
200  300  400  450  500  600  750  1000  1250  1500  1750  2000  2500  3000  3500  3750  4000  4500  5000  5250  5500  5750  6000  6250  <<<< data

这是一些整洁的东西。您继续调用 Input 并填写数组。据我所知,在 Delphi 中这种现象没有等价物。你不能像那样使用 ReadLn,对吧?在德尔福对我来说,你必须

ReadLn(F, S);  //S is a string
z.Delimiter := ' '; //z is a stringlist
z.DelimitedText := S;  //and then breakdown the array

任何想法?谢谢。

4

3 回答 3

3

使用Read代替Readln; 像这样的东西:

var
  ArrLng, Index: Integer;
  Arr: array of Integer;
  F: Text;
begin
  Assign(F, 'your-fie-name');
  OpenFile(F);
  try
    Readln(F, ArrLng);
    SetLength(Arr, ArrLng);
    Index := 0;
    while (not Eof(F)) and (Index < ArrLng) do
    begin
      Read(F, Arr[Index]);
      Inc(Index);
    end;
  finally
    CloseFile(F);
  end;
end;
于 2013-02-26T16:42:24.250 回答
2

拆分字符串通常是更好的方法,无论是否使用 StringList。

但我认为你也可以在那里使用旧的 Pascal 方法。忘记行尾,你可能不需要它。

var F: TextFile; I, J, K: integer;
begin
...
ReadLN(F, J);
for i := 1 to J do 
  Read(F, K);
...
end

但我认为这只适用于小众方法(如内存不足),整体 SplitString 方法会更快。

如果你有多行文件,那么两个链式字符串列表将是恕我直言最简单的方法,前提是这些文件不是 GB 大小的。

https://stackoverflow.com/a/14454614/976391 或利用更多 SL 功能 - https://stackoverflow.com/a/14649862/976391

于 2013-02-26T16:42:30.473 回答
0

The TStringList class and it's LoadFromFile method. It's got delimiter properties as well.

When you are doing Delphi, treat it like .net. If there's something that should be there, it is, you just haven't found it yet. You can chuck the number of values thing away as well.

于 2013-02-26T16:13:20.630 回答