开发者

Pascal - mysterious field increase

开发者 https://www.devze.com 2023-02-16 21:24 出处:网络
I\'m writing a custom structure manipulation program, and I\'ve got the following types: type StrLen = 0..MaxLen;

I'm writing a custom structure manipulation program, and I've got the following types:

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

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

Then I've got the following procedure:

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;

Problem is, when I read into the second object of that type, pos variable, and thus the length field, is getting a mysterious increase by 1. The following code

ReadStr(S1);
ReadStr(S2);

outputs (when I input '123' in both cases):

 pos before entering:0
 123 
 pos after entering:4

 pos before entering:0 
 123 
 pos after entering:5

Will be very gladful, if开发者_开发百科 someone clears situation for me. Thank you in advance.


You're missing some possibly relevant parts of the program. In particular, if this is on Windows then (depending on how you are reading the file) you may have an extra character in the second string because you're stopping at a CR and not processing the following LF.

0

精彩评论

暂无评论...
验证码 换一张
取 消