在Ada中编写文本文件时换行 [英] Newline while writing a text file in Ada

查看:95
本文介绍了在Ada中编写文本文件时换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am opening a text file in Ada with the following code:

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>

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

Note that besides the initial line the cursor is in a second line there with nothing written.

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

Whenever my code writes in the file, this happens:

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

     r

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

It creates another newline instead of appending on the 2nd line like this:

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

如何解决此问题?

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

It's a pointer problem since I read the whole line before, but I try to close and open the file again and the pointer remains in the second line instead of going back to the beginning.

推荐答案

我在Windows上使用GNAT 2012编写了一个快速测试程序,它可以按预期工作。

I threw together a quick test program with GNAT 2012 on Windows and it works as expected.

代码:

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输出文本,这是文件:

I programmatically created the c.txt file, using Put_Line to output the text, this was the contents of the file:

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

我用Cygwin的 od -t x1 来转储文件,并看到它以 0d 0a EOL序列终止,即CR / LF。

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

使用 od 显示以 0d 0a 72 0d 0a 结尾的文件。那是原始的停产期,在其末尾附加了 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.

如果这不是为您而发生,则不清楚您的实际身份在做。 (请注意,在Linux上, 0d 0a 序列将只是 0a 。)

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.)

这篇关于在Ada中编写文本文件时换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆