18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

278 Input and output<br />

Which when run would pr<strong>in</strong>t the follow<strong>in</strong>g results:<br />

Mike 183 Male<br />

Cor<strong>in</strong>na 171 Female<br />

Miranda 74 Female<br />

18.4 Switch<strong>in</strong>g the default <strong>in</strong>put and output streams<br />

It is possible to switch the default <strong>in</strong>put or output stream to another stream us<strong>in</strong>g the follow<strong>in</strong>g procedures <strong>in</strong> the<br />

package <strong>Ada</strong>.Text_Io. The effect is to change the source or s<strong>in</strong>k from which <strong>in</strong>put and output will come from<br />

or go to when us<strong>in</strong>g the normal <strong>in</strong>put and output procedures put and get without a File parameter.<br />

Procedure<br />

Set_Input ( File:<strong>in</strong> File_Type )<br />

Set_OutPut( File:<strong>in</strong> File_Type )<br />

Set_Error ( File:<strong>in</strong> File_Type )<br />

Sets the default<br />

Input file descriptor.<br />

Output file descriptor.<br />

Error file descriptor.<br />

As the file descriptor is of type limited private, it may not be directly assigned. However, an access value of<br />

the file descriptor can be saved. The follow<strong>in</strong>g functions return an access value of the standard <strong>in</strong>put and output<br />

file descriptors:<br />

Function<br />

Standard_Input return File_Access;<br />

Standard_Output return File_Access;<br />

Standard_Error return File_Access;<br />

Returns the access<br />

Value of the <strong>in</strong>put file descriptor.<br />

Value of the output file descriptor.<br />

Value of the error file descriptor.<br />

18.4.1 Putt<strong>in</strong>g it all together<br />

The program first saves an access value to the orig<strong>in</strong>al default <strong>in</strong>put stream. Then it switches the default <strong>in</strong>put<br />

stream to be the file file.txt. After read<strong>in</strong>g and pr<strong>in</strong>t<strong>in</strong>g the contents of this file the default, <strong>in</strong>put stream is<br />

switched back to its orig<strong>in</strong>al value and the contents of the stream are read and written out.<br />

with <strong>Ada</strong>.Text_Io; use <strong>Ada</strong>.Text_Io;<br />

procedure Ma<strong>in</strong> is<br />

Fd : <strong>Ada</strong>.Text_Io.File_Type; --File descriptor<br />

P_St_Fd : <strong>Ada</strong>.Text_Io.File_Access; --Access value of Standard<br />

Ch : Character; --Current character<br />

beg<strong>in</strong><br />

P_St_Fd := Standard_Input; --Acess value of standard fd<br />

Open( File=>Fd, Mode=>In_File, Name=>"file.txt" );<br />

Set_Input( Fd );<br />

while not End_Of_File loop --For each L<strong>in</strong>e<br />

while not End_Of_L<strong>in</strong>e loop -- For each character<br />

Get(Ch); Put(Ch); -- Read / Write character<br />

end loop;<br />

Skip_L<strong>in</strong>e; New_L<strong>in</strong>e; -- Next l<strong>in</strong>e / new l<strong>in</strong>e<br />

end loop;<br />

Close(Fd);<br />

--Close file<br />

Set_Input( P_St_Fd.all );<br />

while not End_Of_File loop --For each L<strong>in</strong>e<br />

while not End_Of_L<strong>in</strong>e loop --For each character<br />

Get(Ch); Put(Ch); --Read / Write character<br />

end loop;<br />

Skip_L<strong>in</strong>e; New_L<strong>in</strong>e;<br />

end loop;<br />

end Ma<strong>in</strong>;<br />

--Next l<strong>in</strong>e / new l<strong>in</strong>e<br />

Note:<br />

Notice how .all has been used to de-reference the access value when us<strong>in</strong>g the procedure<br />

Set_Input.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!