수색…


비고

Ada 표준 라이브러리는 스트리밍 된 파일의 I / O뿐 아니라 텍스트 또는 이진 데이터의 기존 파일의 I / O를 제공합니다. 이진 데이터 파일은 유형의 값 시퀀스가되며 스트림 파일은 여러 유형의 값 시퀀스가 ​​될 수 있습니다.

다른 유형의 요소를 스트림 파일에서 읽고 스트림 파일로 작성하려면 Ada는 유형 속성, 즉 'Read 'Write , 'Input 'Output'Output '으로 표시된 서브 프로그램을 사용합니다. 후자의 두 사람은 Read'Write 가 수행 할 입 / 출력뿐만 아니라 배열 경계, 레코드 판별 및 유형 태그를 읽고 쓰게됩니다.

파일 작성 및 쓰기

절차는 Create , Put_Line , Close 패키지에서 Ada.Text_IO 만들고 파일에 기록하는 데 사용됩니다 file.txt .

with Ada.Text_IO;

procedure Main is
   use Ada.Text_IO;
   F : File_Type;
begin
   Create (F, Out_File, "file.txt");
   Put_Line (F, "This string will be written to the file file.txt");
   Close (F);
end;

결과 파일 file.txt

This string will be written to the file.txt

스트림 만들기 및 쓰기

서브 타입의 스트림 지향 속성은 bare와 binary default 표현을 사용하여 파일에 객체를 기록하기 위해 호출됩니다.

with Ada.Streams.Stream_IO;

procedure Main is
   type Fruit is (Banana, Orange, Pear);
   type Color_Value is range 0 .. 255;
   type Color is record
      R, G, B : Color_Value;
   end record;

   Fruit_Colors : constant array (Fruit) of Color :=
     (Banana => Color'(R => 243, G => 227, B => 18),
      Orange => Color'(R => 251, G => 130, B => 51),
      Pear   => Color'(R => 158, G => 181, B => 94));

   use Ada.Streams.Stream_IO;

   F : File_Type;

begin
   Create (F, Name => "file.bin");
   for C in Fruit_Colors'Range loop
      Fruit'Write (Stream (F), C);
      Color'Write (Stream (F), Fruit_Colors (C));
   end loop;
   Close (F);
end Main;

결과 파일

00000000  00 2e f3 00 e3 00 12 00  01 2e fb 00 82 00 33 00
00000010  02 2e 9e 00 b5 00 5e 00                         

스트림 파일 열기 및 읽기

스트림 작성 및 쓰기 의 데이터를 프로그램으로 다시 읽습니다.

with Ada.Streams.Stream_IO;

procedure Main is
   --
   --  ... same type definitions as in referenced example
   --
   Fruit_Colors : array (Fruit) of Color;

   use Ada.Streams.Stream_IO;

   F : File_Type;
   X : Fruit;
begin
   Open (F, Mode => In_File, Name => "file.bin");
   loop
      Fruit'Read (Stream (F), X);
      Color'Read (Stream (F), Fruit_Colors (X));
   end loop;
exception
   when End_Error =>
      Close (F);
   pragma Assert  -- check data are the same
     (Fruit_Colors (Banana) = Color'(R => 243, G => 227, B => 18) and
      Fruit_Colors (Orange) = Color'(R => 251, G => 130, B => 51) and
      Fruit_Colors (Pear)   = Color'(R => 158, G => 181, B => 94));
end Main;


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow