1

我正在编写一个自定义结构操作程序,我有以下类型:

type
StrLen = 0..MaxLen;
Str = ^StrInst;

StrInst = record
length: StrLen;
data: array [StrPos] of char;
end;

然后我有以下程序:

procedure  ReadStr(var S: Str);
var  pos: StrLen;
begin
      S^.length:=0;
      pos := 0;
      writeln('pos before entering:',pos);
      writeln;
      with  S^  do begin
        repeat
                Inc(pos);
                Read(data[pos]);
        until   (ord(data[pos]) = 13)   or   (pos > MaxLen+1);
        writeln('pos after entering:',pos);
        length := pos-1;
      end;
end;

问题是,当我读入该类型的第二个对象时,pos变量以及长度字段神秘地增加了 1。以下代码

ReadStr(S1);
ReadStr(S2);

输出(当我在两种情况下输入“123”时):

 pos before entering:0
 123 
 pos after entering:4

 pos before entering:0 
 123 
 pos after entering:5

如果有人为我清除情况,将非常高兴。先感谢您。

4

1 回答 1

1

您错过了该程序的一些可能相关的部分。特别是,如果这是在 Windows 上,那么(取决于您读取文件的方式)您可能在第二个字符串中有一个额外的字符,因为您在 CR 处停止并且不处理以下 LF。

于 2011-03-12T09:40:44.480 回答