Ada 编程/库/Ada.Text_IO
此语言功能自 Ada 95 起可用。
Ada.Text_IO 是自 Ada 95 以来 预定义语言环境 的一个单元。
Text_IO 包用于简单 输入输出 (I/O) 的文本格式。
Ada 2005 有一个函数 Get_Line
,它返回一个新创建的字符串,其中包含整行
function
Get_Linereturn
String;
对于旧的 Ada 版本,您需要进行一些操作才能用一次调用获取完整的行。Get_Line
过程获取一行文本,该文本的字符数与 Item 可以容纳的字符数相同,或者直到换行符出现为止(以先出现者为准)。(换行符的表示形式是实现定义的。)它具有以下规范
procedure
Get_Line (Item:out
String; Last:out
Natural);
具体来说,考虑一个 Item,它可以容纳多达 80 个字符。让我们读取两行,第一行包含少于 80 个字符,比如 10 个,第二行至少包含 80 个字符,也许更多。调用
Get_Line (Item, Last);
将读取第一行直到换行符,并消耗它们;Item 的前 10 个字符将填充读取的文本,其余部分是垃圾;Last 将保存最后一个填充的索引。
下一个调用将读取 Item 可以容纳的最大字符数,即 80 个字符;行的其余部分(如果有)和换行符将保持未消耗。因此 Last 将为 Item'First - 1 + 80
。为了消耗行的其余部分,您必须再次调用 Get_Line
。结果将类似于上面两种可能性之一,具体取决于剩余的行长度。如果未读取任何字符(即,当换行符是唯一未读取的东西时),Last
将保存值 Item'First - 1
。
以下示例显示了如何读取整行
with
Ada.Text_IO;with
Ada.Strings.Unbounded;function
Get_Linereturn
Stringis
package
Ustrrenames
Ada.Strings.Unbounded;package
T_IOrenames
Ada.Text_IO; Everything: Ustr.Unbounded_String := Ustr.Null_Unbounded_String; Item : String (1 .. 80); Last : Natural;begin
Get_Whole_Line:loop
T_IO.Get_Line (Item, Last); -- * Ustr.Append (Source => Everything, New_Item => Item (1 .. Last)); -- *exit
Get_Whole_Linewhen
Last < Item'Last; -- **end
loop
Get_Whole_Line;return
Ustr.To_String (Everything);end
Get_Line;
作为一个练习,将 (*) 处的调用更改为
T_IO.Get_Line (Item (11 .. 20), Last); Ustr.Append (Source => Everything, New_Item => Item (11 .. Last));
看看 Item
和 Last
将保存哪些值。然后您将需要哪个条件才能在 (**) 处退出循环?
(当然,这样编码并不是一个非常明智的做法,但作为学习指导,这是可以的。)
原则上它与在控制台中读取相同,但您还需要检查文件末尾
exit
Get_Whole_Linewhen
Last < Item'Lastor
T_IO.End_Of_File (File);
End_of_File 始终为控制台输入的 False(除非您设法输入实现定义的 end_of_file 指示符)。格式良好的文本文件(即使用 Ada.Text_IO
创建的文件)始终将在 end_of_file 指示符之前保存 end_of_line(和 end_of_page)指示符(参见过程 Close
)。
Ada.Text_IO
具有以下嵌套包,用于 标量类型 的输入/输出。唯一的参数是所涉及的类型。
- Decimal_IO
- Enumeration_IO
- Fixed_IO
- Float_IO
- Integer_IO
- Modular_IO
-- Standard Ada library specification -- Copyright (c) 2003-2018 Maxim Reznik <[email protected]> -- Copyright (c) 2004-2016 AXE Consultants -- Copyright (c) 2004, 2005, 2006 Ada-Europe -- Copyright (c) 2000 The MITRE Corporation, Inc. -- Copyright (c) 1992, 1993, 1994, 1995 Intermetrics, Inc. -- SPDX-License-Identifier: BSD-3-Clause and LicenseRef-AdaReferenceManual -- -------------------------------------------------------------------------with
Ada.IO_Exceptions;package
Ada.Text_IOis
type
File_Typeis
limited
private
;type
File_Modeis
(In_File, Out_File, Append_File);type
Countis
range
0 .. implementation_defined;subtype
Positive_Countis
Countrange
1 .. Count'Last; Unbounded :constant
Count := 0; -- line and page lengthsubtype
Fieldis
Integerrange
0 .. implementation_defined;subtype
Number_Baseis
Integerrange
2 .. 16;type
Type_Setis
(Lower_Case, Upper_Case); -- File Managementprocedure
Create (File :in
out
File_Type; Mode :in
File_Mode := Out_File; Name :in
String := ""; Form :in
String := "");procedure
Open (File :in
out
File_Type; Mode :in
File_Mode; Name :in
String; Form :in
String := "");procedure
Close (File :in
out
File_Type);procedure
Delete (File :in
out
File_Type);procedure
Reset (File :in
out
File_Type; Mode :in
File_Mode);procedure
Reset (File :in
out
File_Type);function
Mode (File :in
File_Type)return
File_Mode;function
Name (File :in
File_Type)return
String;function
Form (File :in
File_Type)return
String;function
Is_Open(File :in
File_Type)return
Boolean; -- Control of default input and output filesprocedure
Set_Input (File :in
File_Type);procedure
Set_Output(File :in
File_Type);procedure
Set_Error (File :in
File_Type);function
Standard_Inputreturn
File_Type;function
Standard_Outputreturn
File_Type;function
Standard_Errorreturn
File_Type;function
Current_Inputreturn
File_Type;function
Current_Outputreturn
File_Type;function
Current_Errorreturn
File_Type;type
File_Accessis
access
constant
File_Type;function
Standard_Inputreturn
File_Access;function
Standard_Outputreturn
File_Access;function
Standard_Errorreturn
File_Access;function
Current_Inputreturn
File_Access;function
Current_Outputreturn
File_Access;function
Current_Errorreturn
File_Access; -- Buffer controlprocedure
Flush (File :in
out
File_Type);procedure
Flush; -- Specification of line and page lengthsprocedure
Set_Line_Length (File :in
File_Type; To :in
Count);procedure
Set_Line_Length (To :in
Count);procedure
Set_Page_Length (File :in
File_Type; To :in
Count);procedure
Set_Page_Length (To :in
Count);function
Line_Length (File :in
File_Type)return
Count;function
Line_Lengthreturn
Count;function
Page_Length (File :in
File_Type)return
Count;function
Page_Lengthreturn
Count; -- Column, Line, and Page Controlprocedure
New_Line (File :in
File_Type; Spacing :in
Positive_Count := 1);procedure
New_Line (Spacing :in
Positive_Count := 1);procedure
Skip_Line (File :in
File_Type; Spacing :in
Positive_Count := 1);procedure
Skip_Line (Spacing :in
Positive_Count := 1);function
End_Of_Line (File :in
File_Type)return
Boolean;function
End_Of_Linereturn
Boolean;procedure
New_Page (File :in
File_Type);procedure
New_Page;procedure
Skip_Page (File :in
File_Type);procedure
Skip_Page;function
End_Of_Page (File :in
File_Type)return
Boolean;function
End_Of_Pagereturn
Boolean;function
End_Of_File (File :in
File_Type)return
Boolean;function
End_Of_Filereturn
Boolean;procedure
Set_Col (File :in
File_Type; To :in
Positive_Count);procedure
Set_Col (To :in
Positive_Count);procedure
Set_Line (File :in
File_Type; To :in
Positive_Count);procedure
Set_Line (To :in
Positive_Count);function
Col (File :in
File_Type)return
Positive_Count;function
Colreturn
Positive_Count;function
Line (File :in
File_Type)return
Positive_Count;function
Linereturn
Positive_Count;function
Page (File :in
File_Type)return
Positive_Count;function
Pagereturn
Positive_Count; -- Character Input-Outputprocedure
Get (File :in
File_Type; Item :out
Character);procedure
Get (Item :out
Character);procedure
Put (File :in
File_Type; Item :in
Character);procedure
Put (Item :in
Character);procedure
Look_Ahead (File :in
File_Type; Item :out
Character; End_Of_Line :out
Boolean);procedure
Look_Ahead (Item :out
Character; End_Of_Line :out
Boolean);procedure
Get_Immediate (File :in
File_Type; Item :out
Character);procedure
Get_Immediate (Item :out
Character);procedure
Get_Immediate (File :in
File_Type; Item :out
Character; Available :out
Boolean);procedure
Get_Immediate (Item :out
Character; Available :out
Boolean); -- String Input-Outputprocedure
Get (File :in
File_Type; Item :out
String);procedure
Get (Item :out
String);procedure
Put (File :in
File_Type; Item :in
String);procedure
Put (Item :in
String);procedure
Get_Line (File :in
File_Type; Item :out
String; Last :out
Natural);procedure
Get_Line (Item :out
String; Last :out
Natural);function
Get_Line(File :in
File_Type)return
String;function
Get_Linereturn
String;procedure
Put_Line (File :in
File_Type; Item :in
String);procedure
Put_Line (Item :in
String); -- Generic packages for Input-Output of Integer Typesgeneric
type
Numis
range
<>;package
Integer_IOis
Default_Width : Field := Num'Width; Default_Base : Number_Base := 10;procedure
Get (File :in
File_Type; Item :out
Num; Width :in
Field := 0);procedure
Get (Item :out
Num; Width :in
Field := 0);procedure
Put (File :in
File_Type; Item :in
Num; Width :in
Field := Default_Width; Base :in
Number_Base := Default_Base);procedure
Put (Item :in
Num; Width :in
Field := Default_Width; Base :in
Number_Base := Default_Base);procedure
Get (From :in
String; Item :out
Num; Last :out
Positive);procedure
Put (To :out
String; Item :in
Num; Base :in
Number_Base := Default_Base);end
Integer_IO;generic
type
Numis
mod
<>;package
Modular_IOis
Default_Width : Field := Num'Width; Default_Base : Number_Base := 10;procedure
Get (File :in
File_Type; Item :out
Num; Width :in
Field := 0);procedure
Get (Item :out
Num; Width :in
Field := 0);procedure
Put (File :in
File_Type; Item :in
Num; Width :in
Field := Default_Width; Base :in
Number_Base := Default_Base);procedure
Put (Item :in
Num; Width :in
Field := Default_Width; Base :in
Number_Base := Default_Base);procedure
Get (From :in
String; Item :out
Num; Last :out
Positive);procedure
Put (To :out
String; Item :in
Num; Base :in
Number_Base := Default_Base);end
Modular_IO; -- Generic packages for Input-Output of Real Typesgeneric
type
Numis
digits
<>;package
Float_IOis
Default_Fore : Field := 2; Default_Aft : Field := Num'Digits-1; Default_Exp : Field := 3;procedure
Get (File :in
File_Type; Item :out
Num; Width :in
Field := 0);procedure
Get (Item :out
Num; Width :in
Field := 0);procedure
Put (File :in
File_Type; Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Put (Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Get (From :in
String; Item :out
Num; Last :out
Positive);procedure
Put (To :out
String; Item :in
Num; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);end
Float_IO;generic
type
Numis
delta
<>;package
Fixed_IOis
Default_Fore : Field := Num'Fore; Default_Aft : Field := Num'Aft; Default_Exp : Field := 0;procedure
Get (File :in
File_Type; Item :out
Num; Width :in
Field := 0);procedure
Get (Item :out
Num; Width :in
Field := 0);procedure
Put (File :in
File_Type; Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Put (Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Get (From :in
String; Item :out
Num; Last :out
Positive);procedure
Put (To :out
String; Item :in
Num; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);end
Fixed_IO;generic
type
Numis
delta
<>digits
<>;package
Decimal_IOis
Default_Fore : Field := Num'Fore; Default_Aft : Field := Num'Aft; Default_Exp : Field := 0;procedure
Get (File :in
File_Type; Item :out
Num; Width :in
Field := 0);procedure
Get (Item :out
Num; Width :in
Field := 0);procedure
Put (File :in
File_Type; Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Put (Item :in
Num; Fore :in
Field := Default_Fore; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);procedure
Get (From :in
String; Item :out
Num; Last :out
Positive);procedure
Put (To :out
String; Item :in
Num; Aft :in
Field := Default_Aft; Exp :in
Field := Default_Exp);end
Decimal_IO; -- Generic package for Input-Output of Enumeration Typesgeneric
type
Enumis
(<>);package
Enumeration_IOis
Default_Width : Field := 0; Default_Setting : Type_Set := Upper_Case;procedure
Get (File :in
File_Type; Item :out
Enum);procedure
Get (Item :out
Enum);procedure
Put (File :in
File_Type; Item :in
Enum; Width :in
Field := Default_Width; Set :in
Type_Set := Default_Setting);procedure
Put (Item :in
Enum; Width :in
Field := Default_Width; Set :in
Type_Set := Default_Setting);procedure
Get (From :in
String; Item :out
Enum; Last :out
Positive);procedure
Put (To :out
String; Item :in
Enum; Set :in
Type_Set := Default_Setting);end
Enumeration_IO; -- Exceptions Status_Error :exception
renames
IO_Exceptions.Status_Error; Mode_Error :exception
renames
IO_Exceptions.Mode_Error; Name_Error :exception
renames
IO_Exceptions.Name_Error; Use_Error :exception
renames
IO_Exceptions.Use_Error; Device_Error :exception
renames
IO_Exceptions.Device_Error; End_Error :exception
renames
IO_Exceptions.End_Error; Data_Error :exception
renames
IO_Exceptions.Data_Error; Layout_Error :exception
renames
IO_Exceptions.Layout_Error;private
type
File_Typeis
limited
null
record
;end
Ada.Text_IO;
外部示例
[编辑源代码]- 在以下位置搜索
Ada.Text_IO
的示例:Rosetta Code,GitHub (gists),任何 Alire 包 或者 本 Wikibook。 - 在以下位置搜索与
Ada.Text_IO
相关的帖子:Stack Overflow,comp.lang.ada 或者 任何与 Ada 相关的页面。
FSF GNAT
- 规格:a-textio.ads
- 主体:a-textio.adb
drake