1

我正在使用以下代码在 Ada 中打开一个文本文件:

Open (File => out_parcial_variante1, Name => "c.txt", Mode => append_file);
put(File => out_parcial_variante1, Item=> "r");
close(out_parcial_variante1);

里面的文件结构是这样的:

 01 #510.00:1003.00,512.04:1110.00,515.00:998.00,-98.00,-100.00
 <second empty line, this text is not in the file>

请注意,除了初始行之外,光标位于第二行,没有任何内容。

每当我的代码写入文件时,就会发生这种情况:

     01 #510.00:1003.00,512.04:1110.00,515.00:998.00,-98.00,-100.00

     r

它创建另一个换行符,而不是像这样在第二行追加:

     01 #510.00:1003.00,512.04:1110.00,515.00:998.00,-98.00,-100.00
     r

我该如何解决?

编辑:这是一个指针问题,因为我之前阅读了整行,但我尝试再次关闭并打开文件,指针保留在第二行而不是回到开头。

4

1 回答 1

2

我在 Windows 上用 GNAT 2012 拼凑了一个快速测试程序,它按预期工作。

代码:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Append_Test is

   OPV: File_Type;

begin
   Open (OPV, Append_File, "c.txt");
   Put (OPV, "r");
   Close (OPV);
end Append_Test;

我以编程方式创建了 c.txt 文件,使用 Put_Line 输出文本,这是文件的内容:

01 #510.00:1003.00,512.04:1110.00,515.00:998.00,-98.00,-100.00

I used Cygwin's od -t x1 to dump the file, and saw that it terminated with a 0d 0a EOL sequence, i.e. CR/LF.

Running the above code resulted in a file containing the expected output:

01 #510.00:1003.00,512.04:1110.00,515.00:998.00,-98.00,-100.00
r

Again dumping with od showed the file ending with 0d 0a 72 0d 0a. That's the original EOL, to which is appended the 'r' and another EOL.

If this isn't happening for you, then it's not clear what you're actually doing. (Note that on Linux the 0d 0a sequences would instead be simply 0a.)

于 2013-05-28T16:28:41.533 回答