Introduction to Computers and Programming Prof. I. K. Lundqvist Lecture 3 Mar 15 2004 Ada.Strings.Fixed (A.4.3) • procedure Move (Source : in String;Target : out String;Drop : in Truncation := Error;Justify : in Alignment := Left;Pad : in Character := Space); • function Insert (Source : in String;Before : in Positive;New_Item : in String) return String; • function Delete (Source : in String;From : in Positive;Through : in Natural) return String; Input/Output Input/Output Operations (A.6) ial I/O Text_IO• Related I/O packages • Text files – Formatting input – Formatting output files SequentStreams.Stream_IO Text_IO.Text_Stream Heterogenous I/O Homogeneous Direct I/OFiles (A.7) • External files: Values input from the external environment of the program, or output to the external environment • File (string that gives system defined characteristics such as access rights) Language Defined Types (A.7) • File_Type: associates an object through which the program can access the external file My_File : Ada.Text_IO.File_Type; and Stream_IO type File_Mode is (In_File, Out_File,Append_File); type File_Mode is (In_File, Inout_File,Out_File); Sequential vs. Direct (A.8) • Sequential Files • Direct Files consecutive positions in linear order – index: location of current element(>=1) – current size: number of elements in file Logical View of Text Files (A.10) ) sequence of sequence of sequence of End of Line Page Terminator File Terminator –Name (string) – Form • For Sequential_IO, Text_IO, Wide_Text_IO • For Direct_IO – Sequential_IO – Files viewed as a sequence of values – Transfer occurs in order of appearance –Direct_IO – File viewed as a set of elements occupying Character(s) Line(s) Page(sFile • Terminators are generated – Explicitly by procedures/ functions – Implicitly as part of other operationsAda.Text_IO (A.10.1) procedure Create (File : File_Type;Mode : in File_Mode := Out_File;Name : in String := "";Form : in String := ""); procedure Open (File : File_Type;Mode : in File_Mode;Name : in String;Form : in String := ""); procedure Close (File : File_Type);procedure Delete (File : File_Type);procedure Reset (File : File_Type;Mode : in File_Mode);procedure Reset (File : File_Type); Text Input/Output • • X formats.adb Output to a File 1(2) – Create(Internal_Name, Out_File,"TEST.TXT"); – Put(Internal_Name, ”Some text."); – New_Line(Internal_Name, 2); fileout1.adb Output to a File 2(2) – Set_Output(Internal_Name);Put_Line(”Some other text.");New_Line(2);Put_Line(Standard_Output, ”On screen");Set_Output(Standard_Output); – in out in out in out in out in out in out •Ada and I/O – Embedded real-time systems – Business oriented applications • Package instantiation – Ada.Text_IO.Integer_IO; (generic package) Ada.Integer_Text_IO; (preinstantiated for the type Integer) – Ada.Text_IO.Float_IO; Ada.Float_Text_IO; • Brute force example: Formatted output data • Need 2 file names to read and write to a file – Internal name (used in our program) – External name (used by OS) • Create -- associate names with each other • Writing to a file • Redirecting output • Closing a file Close(Internal_Name);Files, cont. once – Open(My_File, In_File, "CHARACTS.TXT"); – End_Of_File(My_File);TRUE when next char to be read is EOF character – Reset(My_File); – End_Of_Line(My_File)TRUE when next char in buffer is EOL character charin.adb Examples Standard_Input End_Of_File when reading from keyboard stringio.adbintegerin.adb • Example: How to open multiple files at • Input read from a file • File pointer set to beginning of file • Returns • File pointer moved to beginning of file again • Returns multfileout.adb • String Input and Output • Read Integers from file • Read Floating point numbers from file – What has to be changed? • Input from keyboard – Internal filename:
View Full Document