comp.lang.ada
 help / color / mirror / Atom feed
* Usage of Stream Set_Index() function
@ 2022-01-14 18:30 DrPi
  2022-01-14 20:35 ` Niklas Holsti
  0 siblings, 1 reply; 3+ messages in thread
From: DrPi @ 2022-01-14 18:30 UTC (permalink / raw)



Hi,

I'm writing an application where I read a file with a complex structure.
For this, I use Ada.Streams.Stream_IO package.
To read the file, I need to position the file pointer to the correct 
location. I do this using Set_Index().

In ARM A.12.1, it is stated :
For Read and Write with a Positive_Count parameter, the value of the 
current index is set to the value of the Positive_Count parameter plus 
the number of stream elements read or written.

I don't understand the "plus the number of stream elements read or written".

Does this mean one can't go back to beginning of file once data have 
been read ?

Nicolas

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Usage of Stream Set_Index() function
  2022-01-14 18:30 Usage of Stream Set_Index() function DrPi
@ 2022-01-14 20:35 ` Niklas Holsti
  2022-01-15 10:36   ` DrPi
  0 siblings, 1 reply; 3+ messages in thread
From: Niklas Holsti @ 2022-01-14 20:35 UTC (permalink / raw)


On 2022-01-14 20:30, DrPi wrote:
> 
> Hi,
> 
> I'm writing an application where I read a file with a complex structure.
> For this, I use Ada.Streams.Stream_IO package.
> To read the file, I need to position the file pointer to the correct 
> location. I do this using Set_Index().
> 
> In ARM A.12.1, it is stated :
> For Read and Write with a Positive_Count parameter, the value of the 
> current index is set to the value of the Positive_Count parameter plus 
> the number of stream elements read or written.
> 
> I don't understand the "plus the number of stream elements read or 
> written".


For Write, the ARM also says: "The Write procedure with a Positive_Count 
parameter starts writing at the specified index". And then, after the 
data have been written, naturally the current index of the stream has 
been increased, to point at the position after the written data. That is 
the meaning of the "plus" phrase.

And Read analogously. The "plus" phrase explains what the value of the 
current index is _after_ the Read or Write. But both the Read and the 
Write _start_ reading/writing at the index given in the From/To parameters.


> Does this mean one can't go back to beginning of file once data have 
> been read ?


No, Set_Index can do that, whether the last action was writing or 
reading. And the Write/Read with an index parameter can also do that.

But if you have set the current index to the desired position with 
Set_Index, you don't need to use the Read/Write that have a 
Positive_Count (index) parameter.

This code:

    Set_Index (F, I);
    Write (F, Item);

is equivalent to:

    Write (F, Item, I);

and ditto Read.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Usage of Stream Set_Index() function
  2022-01-14 20:35 ` Niklas Holsti
@ 2022-01-15 10:36   ` DrPi
  0 siblings, 0 replies; 3+ messages in thread
From: DrPi @ 2022-01-15 10:36 UTC (permalink / raw)


Le 14/01/2022 à 21:35, Niklas Holsti a écrit :
> On 2022-01-14 20:30, DrPi wrote:
>>
>> Hi,
>>
>> I'm writing an application where I read a file with a complex structure.
>> For this, I use Ada.Streams.Stream_IO package.
>> To read the file, I need to position the file pointer to the correct 
>> location. I do this using Set_Index().
>>
>> In ARM A.12.1, it is stated :
>> For Read and Write with a Positive_Count parameter, the value of the 
>> current index is set to the value of the Positive_Count parameter plus 
>> the number of stream elements read or written.
>>
>> I don't understand the "plus the number of stream elements read or 
>> written".
> 
> 
> For Write, the ARM also says: "The Write procedure with a Positive_Count 
> parameter starts writing at the specified index". And then, after the 
> data have been written, naturally the current index of the stream has 
> been increased, to point at the position after the written data. That is 
> the meaning of the "plus" phrase.
> 
> And Read analogously. The "plus" phrase explains what the value of the 
> current index is _after_ the Read or Write. But both the Read and the 
> Write _start_ reading/writing at the index given in the From/To parameters.
> 
> 
>> Does this mean one can't go back to beginning of file once data have 
>> been read ?
> 
> 
> No, Set_Index can do that, whether the last action was writing or 
> reading. And the Write/Read with an index parameter can also do that.
> 
> But if you have set the current index to the desired position with 
> Set_Index, you don't need to use the Read/Write that have a 
> Positive_Count (index) parameter.
> 
> This code:
> 
>     Set_Index (F, I);
>     Write (F, Item);
> 
> is equivalent to:
> 
>     Write (F, Item, I);
> 
> and ditto Read.

Ok, so Set_Index() works as expected. Nothing special.

Reading the LRM made me understand things incorrectly because of the way 
it is written.

It is written :
32 The Set_Index procedure sets the current index to the specified value.

Then, it is written :
32.1/1 ...
32.2/1 ...
...

Reading this, I thought 32.1/1, 32.2/1 etc lines apply to 32 
(Set_Index() function).
So, I was completely lost to understand the meaning of these lines.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-15 10:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-14 18:30 Usage of Stream Set_Index() function DrPi
2022-01-14 20:35 ` Niklas Holsti
2022-01-15 10:36   ` DrPi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox