File input/output: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
If the file line size exceeds the size of the input string the output file will contain extra new-line characters. |
If the file line size exceeds the size of the input string the output file will contain extra new-line characters. |
||
<lang ada> |
|||
<Ada> |
|||
with Ada.Text_IO; use Ada.Text_IO; |
with Ada.Text_IO; use Ada.Text_IO; |
||
procedure File_IO is |
procedure File_IO is |
||
Line 21: | Line 21: | ||
Close (Output); |
Close (Output); |
||
end File_IO; |
end File_IO; |
||
</ |
</lang> |
||
Note that it is possible to use a version of Get_Line that returns the read line as one string of unspecified length: |
Note that it is possible to use a version of Get_Line that returns the read line as one string of unspecified length: |
||
<lang ada> |
|||
<Ada> |
|||
while not End_Of_File (Input) loop |
while not End_Of_File (Input) loop |
||
Put_Line (Output, Get_Line (Input)); |
Put_Line (Output, Get_Line (Input)); |
||
end loop; |
end loop; |
||
</ |
</lang> |
||
But it is not recommended, because it would make program vulnerable to storage error problems. |
But it is not recommended, because it would make program vulnerable to storage error problems. |
||
The following example reads and writes each file one character at a time. There is no new-line issue. |
The following example reads and writes each file one character at a time. There is no new-line issue. |
||
<lang ada> |
|||
<Ada> |
|||
with Ada.Sequential_Io; |
with Ada.Sequential_Io; |
||
Line 49: | Line 49: | ||
Close(Outfile); |
Close(Outfile); |
||
end File_IO; |
end File_IO; |
||
</ |
</lang> |
||
The following solution uses stream I/O. Any file of Ada.Text_IO can be used to obtain a corresponding stream. Reading and writing streams is more efficient than reading text files directly, because it skips formatting. Note also how End_Error exception is used to avoid End_Of_File. End_Of_File is depreciated as it requires file look-ahead, and thus is much less efficient. |
The following solution uses stream I/O. Any file of Ada.Text_IO can be used to obtain a corresponding stream. Reading and writing streams is more efficient than reading text files directly, because it skips formatting. Note also how End_Error exception is used to avoid End_Of_File. End_Of_File is depreciated as it requires file look-ahead, and thus is much less efficient. |
||
<lang ada> |
<lang ada> |