如果用户按下 Enter 键盘,为什么 Read procedure 会插入 New Line?

why Read procedure insert New Line if user press the Enter keyboard?

我正在尝试使用 "Read" 过程而不是 Free Pascal "ReadLn" 中的 IDE 但是当我在输入它的值后按回车键时,我得到了一个新行,比如"readln" 确实....

program test;
uses
  crt;
var
  AName: string[20];

function Complete_Rectangle(ALength_NewValue, ALength_Rectangle, ALength_Pref: byte): string;
const
  Stars : string = '**';
var
  Space_Str: string;
  I,
  Space_Needed : byte;
begin
  Space_Needed := ALength_Rectangle - ALength_Pref; (* Calculate Needed Space.... *)
  Space_Str  := '';

  for I:=1 to Space_Needed -(ALength_NewValue + 2) do  (* 2 represent the length of Stars Constant *)
  begin
    Space_Str := Space_Str + ' ';
  end;

  Complete_Rectangle := Space_Str + Stars;
end;

begin
  clrscr;
  writeln('**********************************************************************');
  writeln('**                                                                  **');
    write('**  [*] add your Name here: '); read(AName); writeln(Complete_Rectangle(length(AName), 70, 28));
    (* 70 represent the length of rectangle spaces and 28 represent the length of this prefix text "**  [*] add your Name here: " *)
  writeln('**                                                                  **');
  writeln('**********************************************************************');
  readkey;
end.

是否可以创建我自己的自定义阅读来解决上述问题? 我这里有这段代码,但它似乎不能正常工作......:[=​​12=]

procedure Lire(AVariant: Variant);
var
  I: byte;
  AKey: Char;
  AReadStr: String;
begin
  I := 1;
  AReadStr := '';
  AKey := char('');

  repeat
    AKey := ReadKey;
    if not (AKey = #13) then
    begin
      AReadStr[I] := AKey;
      write(AKey);
      inc(I);
    end;
  Until AKey = #13;
  case varType(AVariant) of
    varString: begin AVariant := AReadStr; end;
    varInteger: begin AVariant := StrToInt(AReadStr); end;
    varByte: begin AVariant := StrToInt(AReadStr); end;
    varBoolean: begin AVariant := StrToBool(AReadStr); end;
    (* ..... and so on *)
  end;


end;

Read 和 Readln 语句对于从键盘获取输入从文件读取数据

关于 Readln 来自文档:

reads one or more values from a file F, and stores the result in V1, V2, etc. After that it goes to the next line in the file.

关于从文档中阅读

reads one or more values from a file F, and stores the result in V1, V2, etc.

所以当从文件中读取时,readln 会转到下一行,而 read 则不会。但这不是控制台输入的预期行为。

如果您真的想将光标定位在屏幕上,我建议您使用旧的 gotXY 命令,它在 FreePascal 中也可用。 Link to gotoXY

例如:

write('**  [*] add your Name here: '); 
read(AName); 
gotoXY(28 + length(AName)+ 1, WhereY-1); // Repositionate the cursor to the right Position.
writeln(Complete_Rectangle(length(AName), 70, 28));