阿达阵列存储界字符串从.txt文件中获取 [英] Ada Array store unbounded String getting from .txt file
问题描述
林具有存储无限字符串数组,如果I'mm得到它为.txt文件的麻烦。这是我很难codeD数据:
数据:An_Array:=(1 =>(名称=> +9 - 乔),
2 => (名称=> +8 - 特丽莎),
3 => (名称=> +7 - RR),
4 => (名称=> +6 - 简),
5 => (名称=> +5 - 多伊));
如果我想从获取数据什么 .TXT
文件?
与Ada.Strings.Unbounded;使用Ada.Strings.Unbounded;
与Ada.Text_IO;使用Ada.Text_IO;与Ada.Containers.Generic_Array_Sort;程序主要是 功能+(S:字符串)返回Unbounded_String重命名To_Unbounded_String; 键入A_Composite是
记录
名称:Unbounded_String; 结束记录; 功能< (L,R:A_Composite)返回布尔是
开始
返回L.Name< R.Name;
结束< 程序PUT_LINE(C:A_Composite)是
开始
PUT_LINE(To_String(C.Name));
结束PUT_LINE; 键入An_Array是阵列(自然科学范围和LT;>)A_Composite的; 程序排序是新Ada.Containers.Generic_Array_Sort(自然,A_Composite,An_Array); 数据:An_Array:=(1 =>(名称=> +9 - 乔),
2 => (名称=> +8 - 特丽莎),
3 => (名称=> +7 - RR),
4 => (名称=> +6 - 简),
5 => (名称=> +5 - 多伊));
开始
Ada.Text_IO.Open(文件=>文件,
模式=> Ada.Text_IO.In_File,
名称=> highscore.txt); 虽然不Ada.Text_IO.End_Of_File(文件)循环
宣布
行:字符串:= Ada.Text_IO.Get_Line(文件);
开始 - 获取从.txt数据,我希望它sotre到数据:An_Array
结束;
结束循环;
Ada.Text_IO.Close(文件); 排序(数据);
因为我在Data'Range循环
PUT_LINE(数据(Ⅰ));
结束循环;
最终主;
在阿达,任何特定的阵列有一个固定的长度;你的数据
的长度为5,即使你的键入的 An_Array
可用于用你喜欢的任何长度声明数组(在合理范围内!您的计算机只有固定的内存量)。
我猜你要处理的文件,你不事先知道有多少行也有。
您可以通过宣称这样做
数据:An_Array(1 .. 1000);
和保持的你已经使用了多少条目数:
最后:整数:= 0; - 万一文件不包含任何行!
开始
虽然不Ada.Text_IO.End_Of_File(文件)循环
最后:=最后+ 1;
数据(最后):=
(名称=> Ada.Strings.Unbounded.To_Unbounded_String
((文件)));
结束循环;
当循环结束数据(1 ..姓)
包含您的数据(数据(最近+ 1 .. 1000)
包含空字符串)。
但是,如果在你的文件1001项?
您可以尝试 Ada.Containers.Vectors
( ARM A.18.2 )和 Ada.Containers.Generic_Sort
( ARM 18.29(9.1)) - 你会必须制定出如何指定之前
,并编写自己的交换
程序。
Im having a trouble storing unbounded string to an array if I'mm getting it to .txt file. Here is my hardcoded data:
Data : An_Array := (1 => (Name => +"9 - Joe"),
2 => (Name => +"8 - Trisha "),
3 => (Name => +"7 - RR "),
4 => (Name => +"6 - Jane "),
5 => (Name => +"5 - Doe "));
What if I want to get the data from a .txt
file?
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Generic_Array_Sort;
procedure Main is
function "+" (S : String) return Unbounded_String renames To_Unbounded_String;
type A_Composite is
record
Name : Unbounded_String;
end record;
function "<" (L, R : A_Composite) return Boolean is
begin
return L.Name < R.Name;
end "<";
procedure Put_Line (C : A_Composite) is
begin
Put_Line (To_String (C.Name));
end Put_Line;
type An_Array is array (Natural range <>) of A_Composite;
procedure Sort is new Ada.Containers.Generic_Array_Sort (Natural, A_Composite, An_Array);
Data : An_Array := (1 => (Name => +"9 - Joe"),
2 => (Name => +"8 - Trisha "),
3 => (Name => +"7 - RR "),
4 => (Name => +"6 - Jane "),
5 => (Name => +"5 - Doe "));
begin
Ada.Text_IO.Open (File => File,
Mode => Ada.Text_IO.In_File,
Name => "highscore.txt");
while not Ada.Text_IO.End_Of_File (File) loop
declare
Line :String := Ada.Text_IO.Get_Line (File);
begin
-- get data from .txt and I want it to sotre to Data : An_Array
end;
end loop;
Ada.Text_IO.Close (File);
Sort (Data);
for I in Data'Range loop
Put_Line (Data (I));
end loop;
end Main;
In Ada, any particular array has a fixed length; your Data
has a length of 5, even though your type An_Array
can be used to declare arrays with any length you like (within reason! your computer only has a fixed amount of memory).
I’m guessing that you want to deal with files where you don’t know beforehand how many lines there are.
You could do this by declaring
Data : An_Array (1 .. 1000);
and keeping a count of how many entries you’ve used:
Last : Integer := 0; -- in case the file contains no lines!
begin
while not Ada.Text_IO.End_Of_File (File) loop
Last := Last + 1;
Data (Last) :=
(Name => Ada.Strings.Unbounded.To_Unbounded_String
((File)));
end loop;
and when the loop ends Data (1 .. Last)
contains your data (Data (Last + 1 .. 1000)
contains empty strings).
But what if there are 1001 entries in your file?
You could try Ada.Containers.Vectors
(ARM A.18.2) and Ada.Containers.Generic_Sort
(ARM 18.29(9.1)) - you’d have to work out how to specify Before
, and to write your own Swap
procedure.
这篇关于阿达阵列存储界字符串从.txt文件中获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!