Ada 程式設計/庫/Ada.Sequential_IO
外觀
此語言功能從 Ada 95 開始提供。
Ada.Sequential_IO 是 預定義語言環境 自 Ada 95 以來的一部分。
並非所有讀取或寫入的資料都以文字格式儲存。為了儲存或程序間通訊,以二進位制格式儲存資料在空間和處理時間方面更有效率。
用於順序資料的二進位制輸入和輸出(即逐個讀取/寫入)由泛型 Ada 包 Ada.Sequential_IO 管理。
泛型包必須使用某些 Element_Type 例項化,並提供一個受限的私有 File_Type。
檔案模式 In_File 或 Out_File 在開啟或建立時建立。
該包提供基本的輸入/輸出 (I/O) 操作
Open、CreateClose、Delete和ResetRead、Write
此外,該包還提供狀態和資訊函式
Is_OpenEnd_Of_FileName、Mode和Form
一個重要的限制是,檔案的每個元素都必須是相同的元素型別。
例如,假設一位學生在一個 10 米乘 10 米的地塊中收集了樹木的資訊,並希望從資料中生成一份報告。
以下是一個讀取先前記錄的順序資料的簡單程式
withAda.Text_IO; -- Text I/O librarywithAda.Float_Text_IO; -- Float I/O librarywithAda.Sequential_IO; -- Sequential I/O libraryprocedureTree_Reportis-- First, define the contents of the data recordtypeTree_RecordisrecordCommon_Name : String(1..20) := (others=> ' '); Scientific_Name : String(1..20) := (others=> ' '); Diameter : Float := 0.0; -- at breast height (DBH)endrecord; -- Tree_Record -- Now, instantiate the I/O package for the data recordpackageTree_IOisnewAda.Sequential_IO(Tree_Record); Tree_File : Tree_IO.File_Type; -- Input data file Tree_Data : Tree_Record; -- Individual data recordbegin-- 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:whilenotTree_IO.End_Of_File(Tree_File)loopTree_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;endloopProcess_Tree_Data; -- All data records have been read, and end_of_file detected. Tree_IO.Close(Tree_File); -- Done, close fileendTree_Report;
-- Standard Ada library specification -- Copyright (c) 2003-2018 Maxim Reznik <reznikmm@gmail.com> -- 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 -- -------------------------------------------------------------------------withAda.IO_Exceptions;generictypeElement_Type (<>)isprivate;packageAda.Sequential_IOistypeFile_Typeislimitedprivate;typeFile_Modeis(In_File, Out_File, Append_File); -- File managementprocedureCreate (File :inoutFile_Type; Mode :inFile_Mode := Out_File; Name :inString := ""; Form :inString := "");procedureOpen (File :inoutFile_Type; Mode :inFile_Mode; Name :inString; Form :inString := "");procedureClose (File :inoutFile_Type);procedureDelete (File :inoutFile_Type);procedureReset (File :inoutFile_Type; Mode :inFile_Mode);procedureReset (File :inoutFile_Type);functionMode (File :inFile_Type)returnFile_Mode;functionName (File :inFile_Type)returnString;functionForm (File :inFile_Type)returnString;functionIs_Open (File :inFile_Type)returnBoolean; -- Input and output operationsprocedureRead (File :inFile_Type; Item :outElement_Type);procedureWrite (File :inFile_Type; Item :inElement_Type);functionEnd_Of_File (File :inFile_Type)returnBoolean; -- Exceptions Status_Error :exceptionrenamesIO_Exceptions.Status_Error; Mode_Error :exceptionrenamesIO_Exceptions.Mode_Error; Name_Error :exceptionrenamesIO_Exceptions.Name_Error; Use_Error :exceptionrenamesIO_Exceptions.Use_Error; Device_Error :exceptionrenamesIO_Exceptions.Device_Error; End_Error :exceptionrenamesIO_Exceptions.End_Error; Data_Error :exceptionrenamesIO_Exceptions.Data_Error;privatetypeFile_Typeislimitednullrecord;endAda.Sequential_IO;
外部示例
[編輯原始碼]- 在以下網站搜尋
Ada.Sequential_IO的示例:Rosetta Code、GitHub (gists)、任何 Alire 包 或 本華夏公益教科書。 - 在以下網站搜尋與
Ada.Sequential_IO相關的帖子:Stack Overflow、comp.lang.ada 或 任何與 Ada 相關的頁面。
FSF GNAT
- 規範:a-sequio.ads
- 主體:a-sequio.adb
drake
