我是艾达的新手。语法让我失望。我在 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/