跳转到内容

Ada 编程/库/Ada.Sequential_IO

来自维基教科书,开放的书籍,开放的世界

Ada. Time-tested, safe and secure.
Ada。经久耐用、安全可靠。

此语言功能从 Ada 95 开始提供。

Ada.Sequential_IO预定义语言环境 自 Ada 95 以来的一部分。

并非所有读取或写入的数据都以文本格式存储。为了存储或进程间通信,以二进制格式存储数据在空间和处理时间方面更有效率。

用于顺序数据的二进制输入和输出(即逐个读取/写入)由泛型 Ada 包 Ada.Sequential_IO 管理。

泛型包必须使用某些 Element_Type 实例化,并提供一个受限的私有 File_Type

文件模式 In_FileOut_File 在打开或创建时建立。

该包提供基本的输入/输出 (I/O) 操作

  • OpenCreate
  • CloseDeleteReset
  • ReadWrite

此外,该包还提供状态和信息函数

  • Is_Open
  • End_Of_File
  • NameModeForm

一个重要的限制是,文件的每个元素都必须是相同的元素类型。

Ada 代码示例

[编辑 | 编辑源代码]

例如,假设一位学生在一个 10 米乘 10 米的地块中收集了树木的信息,并希望从数据中生成一份报告。
以下是一个读取先前记录的顺序数据的简单程序

  with Ada.Text_IO; --  Text I/O library
  with Ada.Float_Text_IO; --  Float I/O library
  with Ada.Sequential_IO; --  Sequential I/O library

  procedure Tree_Report is
   
  --  First, define the contents of the data record

     type Tree_Record is 
        record
           Common_Name     : String(1..20) := (others => ' ');
           Scientific_Name : String(1..20) := (others => ' ');
           Diameter        : Float         := 0.0; --  at breast height (DBH)
        end record; --  Tree_Record
  
--  Now, instantiate the I/O package for the data record

     package Tree_IO is new Ada.Sequential_IO(Tree_Record);
  
     Tree_File : Tree_IO.File_Type; --  Input data file
  
     Tree_Data : Tree_Record; --  Individual data record
  
  begin --  Tree_Report
  
--  Open the input data file.

     Tree_IO.Open(File => Tree_File,
                  Mode => Tree_IO.In_File,
                  Name => "tree.dat");
  
  --  Write report heading
     Ada.Text_IO.Set_Col(To =>  1); Ada.Text_IO.Put("COMMON NAME");
     Ada.Text_IO.Set_Col(To => 21); Ada.Text_IO.Put("SCIENTIFIC NAME");
     Ada.Text_IO.Set_Col(To => 41); Ada.Text_IO.Put("DBH");
     Ada.Text_IO.New_Line;
  
  Process_Tree_Data:
     while not Tree_IO.End_Of_File(Tree_File) loop
     
        Tree_IO.Read(Tree_File, Tree_Data); --  Read tree data
     
     --  Write tree inventory
        Ada.Text_IO.Put(Tree_Data.Common_Name);
        Ada.Text_IO.Put(Tree_Data.Scientific_Name);
        Ada.Float_Text_IO.Put(Tree_Data.Diameter,Fore=>2,Aft=>2,Exp=>0);
        Ada.Text_IO.New_Line;
        
     end loop Process_Tree_Data;
  
--  All data records have been read, and end_of_file detected.

     Tree_IO.Close(Tree_File); --  Done, close file
  
  end Tree_Report;
--                     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;
generic
   type Element_Type (<>) is private;
package Ada.Sequential_IO is

   type File_Type is limited private;

   type File_Mode is (In_File, Out_File, Append_File);

   --  File management

   procedure 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;

   --  Input and output operations

   procedure Read  (File : in File_Type; Item : out Element_Type);
   procedure Write (File : in File_Type; Item : in Element_Type);

   function End_Of_File (File : in File_Type) return Boolean;

   --  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;

private

   type File_Type is limited null record;

end Ada.Sequential_IO;

另请参阅

[编辑 | 编辑源代码]

维基教科书

[编辑 | 编辑源代码]

外部示例

[编辑源代码]

Ada 参考手册

[编辑 | 编辑源代码]

开源实现

[编辑 | 编辑源代码]

FSF GNAT

drake

华夏公益教科书