Newline while writing a text file in Ada

淺唱寂寞╮ 提交于 2019-12-12 05:05:22

问题


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

The file as a structure like this inside:

 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

How do I fix this?

EDIT: 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.


回答1:


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

Code:

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;

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

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



来源:https://stackoverflow.com/questions/16795122/newline-while-writing-a-text-file-in-ada

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!