1

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.

Marc C
  • 8,664
  • 1
  • 24
  • 29
user697110
  • 589
  • 10
  • 25
  • I don't know what you mean by "It's a pointer problem"? And what OS? What compiler? Have you verified that before opening the file to which you're appending that it doesn't end with two consecutive EOLs (LF, CF/LF, or CR)? – Marc C May 28 '13 at 15:40
  • By pointer problem I mean that I do a get_line before to read a whole line of the file and creates a new line leaving the cursor in the beginning of the second line, something I don't want to, I want it to go to the beginning of the file (and for some reason closing and opening the file doesn't solve this). And yes I verified the file for the EOL's. – user697110 May 28 '13 at 15:49
  • From Ada 2012 LRM, A.10.2: `If the mode is Append_File, it is implementation defined whether a page terminator will separate preexisting text in the file from the new text to be written.` I guess this is what's happening here. – flyx May 28 '13 at 15:52
  • So I assume there is no way to avoid this then? – user697110 May 28 '13 at 15:54
  • If I'm understanding you correctly, reading from the file has no effect on its contents. I.e. by opening it in Append mode as your code indicates, it's going to *append* to the file, after the text that's remains present within it. – Marc C May 28 '13 at 15:56
  • You could [read the whole file](http://rosettacode.org/wiki/Read_entire_file#Ada) and then write the contents followed by your addition back into the file with `Out_File`. Of course, this is only a good idea if the actual file is not too large. – flyx May 28 '13 at 15:59
  • Otherwise, try using `Sequential_IO` or `Direct_IO` with `String`. The `Append_File` rule I posted does not apply to them. – flyx May 28 '13 at 16:06
  • Well I kind used an work around in which I just deleted everything in the file and rewrote everything with the "r" as I wanted. Thanks for the help anyway. – user697110 May 28 '13 at 16:13

1 Answers1

2

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

Marc C
  • 8,664
  • 1
  • 24
  • 29