Delphi 被';写';帕斯卡

Delphi 被';写';帕斯卡,delphi,pascal,Delphi,Pascal,我知道: write('test')将在屏幕上打印“测试”, 当 将“测试”写入文件outfile.txt 下面的代码尝试执行将字符打印到屏幕和将字符写入文件的操作 program doboth(input, output) begin assign(input, 'infile.txt'); assign(output, 'outfile.txt'); reset(input); rewrite(output); procedure getcharfrominput;

我知道:
write('test')将在屏幕上打印“测试”,
当

将“测试”写入文件outfile.txt

下面的代码尝试执行将字符打印到屏幕和将字符写入文件的操作

program doboth(input, output)
begin
  assign(input, 'infile.txt');
  assign(output, 'outfile.txt');
  reset(input);
  rewrite(output);
  procedure getcharfrominput;

  begin
  {procedure to read char one by one from infile and write the read char}
  { to out file and print on the screen in the same time}
      ...
      write(ch);         {ch is expected to be printed on the screen}
      write(output, ch); {ch is expected to be wrote to the outfile.txt}
      ...
   end;{procedure getcharfrominput}

begin
   getcharfrominput;
end;{program doboth}
但结果是,读取的字符被写入outfile.txt两次,而屏幕没有任何输出(这意味着
write(ch);
也将字符写入文件而不是屏幕)

然后,我更改了“input”和“output”变量的声明,并得到以下代码:

program doboth;         {remove input output}
var input, output:text; {declare input and output here}
begin                   {the rest of the code is the same}
  assign(input, 'infile.txt');
  assign(output, 'outfile.txt');
  reset(input);
  rewrite(output);
  procedure getcharfrominput;

  begin
  {procedure to read char one by one from infile and write the read char}
  { to out file and print on the screen in the same time}
      ...
      write(ch);         {ch is expected to be printed on the screen}
      write(output, ch); {ch is expected to be wrote to the outfile.txt}
      ...
   end;{procedure getcharfrominput}

begin
   getcharfrominput;
end;{program doboth}

然后我得到了我想要的结果。但是我仍然不知道为什么这个问题可以通过这样的操作来解决。

不久前,程序用来从键盘获取输入,并将输出写入控制台(文本屏幕)或文本文件。 主程序的参数输入和输出明确了这一点,并提供了“重定向”输入和输出的方法。 即,您可以将文本文件与输入和/或输出相关联。就像Ianx86在他的程序中所做的那样

所以, Readln(ch)从输入读取数据。 Writeln(ch)写入输出

现在,如果将输出与文本文件关联(“重定向”它), Writeln(ch)和Writeln(output,ch)变得相同,即。 Writeln(ch)不再写入控制台,而是写入文本文件

备注:

有人提到(输入、输出)作为主程序的参数被忽略。 这似乎是正确的。 如果将其替换为(inp,outp), “input”和“output”仍然可以用作全局变量,“inp”和“outp”是未声明的标识符。
这是一个编译器错误。

不久前,程序用于从键盘获取输入,并将输出写入控制台(文本屏幕)或文本文件。 主程序的参数输入和输出明确了这一点,并提供了“重定向”输入和输出的方法。 即,您可以将文本文件与输入和/或输出相关联。就像Ianx86在他的程序中所做的那样

所以, Readln(ch)从输入读取数据。 Writeln(ch)写入输出

现在,如果将输出与文本文件关联(“重定向”它), Writeln(ch)和Writeln(output,ch)变得相同,即。 Writeln(ch)不再写入控制台,而是写入文本文件

备注:

有人提到(输入、输出)作为主程序的参数被忽略。 这似乎是正确的。 如果将其替换为(inp,outp), “input”和“output”仍然可以用作全局变量,“inp”和“outp”是未声明的标识符。
这是一个编译器错误。

Rewrite
会截断文件,并且在这两种情况下都会截断输入和输出文件。无法读取已被截断的文件(因为它没有内容)。您的代码当前显示创建两个新文件。如果其中一个存在,请删除所有内容。现在,从一个空文件中读取,然后写入另一个。这显然无法按照您的意愿工作。
Rewrite
说明创建一个新文件并打开它。您想在输入文件上使用
Reset
。谢谢您的通知。在我的源文件中是“重置”的,我只是在写这个问题中的代码时犯了一个错误@KenWhite如果你想让我们帮助你解决代码问题,请发布你真正的代码。边做边补可能会引入新的错误(正如您刚才评论的那样)并隐藏实际问题。请回答您的问题,并提供您正在使用的实际代码。如果您不想编写可编译的代码(例如,没有
),可能没有人愿意研究您的问题。看,这是一系列XY问题。您应该学习使用调试器并阅读帮助(docwiki.embarcadero.com),以便理解Pascal语法。
Rewrite
截断文件,并且在这两种情况下都截断输入和输出文件。无法读取已被截断的文件(因为它没有内容)。您的代码当前显示创建两个新文件。如果其中一个存在,请删除所有内容。现在,从一个空文件中读取,然后写入另一个。这显然无法按照您的意愿工作。
Rewrite
说明创建一个新文件并打开它。您想在输入文件上使用
Reset
。谢谢您的通知。在我的源文件中是“重置”的,我只是在写这个问题中的代码时犯了一个错误@KenWhite如果你想让我们帮助你解决代码问题,请发布你真正的代码。边做边补可能会引入新的错误(正如您刚才评论的那样)并隐藏实际问题。请回答您的问题,并提供您正在使用的实际代码。如果您不想编写可编译的代码(例如,没有
),可能没有人愿意研究您的问题。看,这是一系列XY问题。您应该学习使用调试器,并学习阅读帮助(docwiki.embarcadero.com),以便理解Pascal语法。在Turbo Pascal中,“program”的参数总是被忽略的,文档也清楚地说明了这一点。Iirc,TP允许这些参数的原因只是为了避免程序因为它们在那里而无法编译。我不认为有文件证明的对某些东西缺乏支持就可以称为编译器错误。输入和输出声明系统在旧系统上很重要,比如允许在程序运行之前绑定文件描述符的VM。TP仅为x86,因此在标准中仍然是黄金的遗产并不重要。特别是因为TP源于UCSD Pascal而非ISO。在Turbo Pascal中,“program”的参数总是被忽略的,文档也清楚地说明了这一点。Iirc,TP允许这些参数的原因只是为了避免程序因为它们在那里而无法编译。我不认为对某些东西缺乏支持就可以被称为编译器错误
program doboth;         {remove input output}
var input, output:text; {declare input and output here}
begin                   {the rest of the code is the same}
  assign(input, 'infile.txt');
  assign(output, 'outfile.txt');
  reset(input);
  rewrite(output);
  procedure getcharfrominput;

  begin
  {procedure to read char one by one from infile and write the read char}
  { to out file and print on the screen in the same time}
      ...
      write(ch);         {ch is expected to be printed on the screen}
      write(output, ch); {ch is expected to be wrote to the outfile.txt}
      ...
   end;{procedure getcharfrominput}

begin
   getcharfrominput;
end;{program doboth}