ada - 如何读取每个单词并将其存储到变量中以供使用?

标签 ada

我是艾达的新手。语法让我失望。我在 Java 上有 6 年的经验,这与我们在 Java 中所做的类似,但我完全无法让它工作。我正在使用 learn.adacore.com 进行学习。

with Ada.Text_IO; Use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;

procedure TextFile is

   F      : File_Type;
   File_Name : constant String := "Store.txt";
begin
   
   Open (F, In_File, File_Name);
   
   while not End_Of_File (F) loop
      Put_Line (Get_Line (F));
   end loop;
   Close (F);
   
end TextFile;

这是我的名为 Store.txt 的文本文件

Code    Department  Name/Vendor  Title          ID      Payrate
IL      Sales       John         Sales_person   1378    25.46

最佳答案

如果您不介意不同 Ada 编译器之间的可移植性,您可以使用包 GNAT.String_Split 将整行拆分为分隔字符串值的数组:

with Ada.Text_IO;       use Ada.Text_IO;
with GNAT.String_Split; use GNAT.String_Split;

procedure TextFile is
   File   : File_Type;
   Tokens : Slice_Set;
begin
   Open (File, In_File, "Store.txt");
   -- Skip the file header
   Skip_Line (File);
   -- Read the data
   while not End_Of_File (File) loop
      -- Split the line from the file on array which contains separated
      -- words. Treat multiple spaces as a single separator (don't
      -- create empty elements).
      Create (Tokens, Get_Line (File), " ", Multiple);
      -- Print each of the array's values
      for I in 1 .. Slice_Count (Tokens) loop
         Put_Line (Slice (Tokens, I));
      end loop;
   end loop;
   Close (File);
end TextFile;

关于ada - 如何读取每个单词并将其存储到变量中以供使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64565722/

相关文章:

string - Ada - (Streams) 如何在事先不知道字符串长度的情况下正确调用 String'Read()

generics - 包含对基于该记录的通用包实例化的访问的记录

testing - Ada:使用aunit

apache - 在 Unix 网络服务器上运行的 Ubuntu 编译程序

c - 如何创建 ada lib.a 并链接到 C

c - GPRBuild 不编译 C 文件

Ada - 约束错误

format - 如果没有被告知,什么可以使简单 ada 程序的输出缩进?

integer - 操作数的类型无效 "+"Ada 95

ada - Ada 2012 中的自定义条件失败消息