从文件中的一行获取变量

时间:2016-11-25 19:27:40

标签: ada

我想知道你们中是否有人能为我回答一个简单的问题。我目前正在处理记录,在我的程序中,我需要它来了解我正在导入的文件行包含的内容。我的问题在于我不知道如何将线“拆分”为实际变量。例如,该行是

22134.09 Kia Bernice

如何让程序知道第一部分22134.09是可变价格,起亚是变量公司而Bernice是变量模型,然后将它们全部排序成记录?

type PriceCompModel is record
price : Float range 1.0..99999.99;
company : String (1..CompNameLength);
Model : String (1..ModelNameLength);

感谢。

已编辑的代码

    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 Testexercise is

   type Inventory is Record

      CarPrice : Float := 0.0;
      CarType  : String (1..40);
      -- CarType will include both the model and company
   end record;

   InventoryItem  : Inventory;
   ImportedFile   : File_Type;
   FileName       : String := "Cars.txt";
   WordsFromFile  : String(1..40);
   LengthofWords  : Integer ;
   PriceofCar     : Float := 0.0;
   LoopCount      : Integer := 1;

   type Cars is array (1..12) of Inventory;

   begin

      Open(File => ImportedFile, Mode => In_File, Name => FileName);

      --for I in 1..12 loop

      while LoopCount /= 12 loop

         Get(File => ImportedFile, Item => InventoryItem.CarPrice);

         Get_Line(File => ImportedFile, Item => WordsFromFile, Last=> LengthofWords);       

         Put (Integer(InventoryItem.CarPrice),1);

         Put (WordsFromFile(1..LengthofWords));

         New_Line;

         LoopCount := LoopCount + 1;

         InventoryItem.CarType := WordsFromFile;      


      end loop;
     close(ImportedFile);



   end Testexercise;

所以我尝试在循环中执行此操作

for I in 1..12 loop
    Cars := Inventory;
   end loop;

在我设置

后,这最终为我工作 汽车:汽车;

for I in 1..12 loop
Car(I) := Inventory;
end loop;

2 个答案:

答案 0 :(得分:4)

定义包含信息的记录时,需要考虑许多因素。

创建一个名为float的子类型或一个命名浮点类型将是很有用的,这样I / O例程就可以检查price组件的输入值。

String类型的字段必须限制为预定义的大小。这意味着所有"公司"字符串必须大小相同,并且所有" model"尽管模型字符串的长度可能与公司字符串的长度不同,但字符串的大小必须相同。如果公司和/或模型的名称可能有所不同,那么您应该考虑使用有界字符串(Ada语言参考手册第A.4.4节)或无界字符串(Ada语言参考手册第A.4.5节)。

如果输入文件的字符串大小固定,您可以使用Ada.Text_IO.Text_Streams(Ada语言参考手册第A.12.2节)来读取记录的每个字段。如果字符串可以是不同的大小,那么您将需要使用Ada.Text_IO手动读取和解析每个字段。

-- Read record data from a file
with Ada.Text_Io; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;

procedure read_record is
   type Prices is delta 0.01  digits 7 range 0.0..99999.99;
   type Auto_Inventory is record
      Price   : Prices := 0.0;
      Company : Unbounded_String := Null_Unbounded_String;
      Model   : Unbounded_String := Null_Unbounded_String;
   end record;
   package AI_IO is new Ada.Text_IO.Decimal_IO(Prices);
   use AI_IO;

   Inventory_Item : Auto_Inventory;
   The_File  : File_Type;
   File_Name : String := "inventory.txt";
   Inpt_Str  : String(1..1024);
   Length    : Natural;
   Start, Finis : Positive;
begin
   Open(File => The_File,
        Mode => In_File,
        Name => File_Name);
   Get(File => The_File,
       Item => Inventory_Item.Price);
   Get_Line(File => The_File,
            Item => Inpt_Str,
            Last => Length);
   Close(The_File);
   Start := Index_Non_Blank(Source => Inpt_Str(1..Length));
   Finis := Start;
   while Finis < Length and then Inpt_Str(Finis) /= ' ' loop
      Finis := Finis + 1;
   end loop;
   Inventory_Item.Company := To_Unbounded_String(Inpt_Str(Start..Finis));
   Start := Index_Non_Blank(Inpt_Str(Finis + 1..Length));
   Inventory_Item.Model := To_Unbounded_String(Inpt_Str(Start..Length));
   Put_Line("Price: " & Prices'Image(Inventory_Item.Price));
   Put_Line("Company: " & To_String(Inventory_Item.Company));
   Put_Line("Model: " & To_String(Inventory_Item.Model));
end read_record;

如果要读取包含许多记录的文件,则需要在某种容器中收集信息。以下示例使用通用包Ada.Containers.Vectors中的Vector。

-- Auto Inventory Package specification
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

package Auto_Inventory is
   type Prices is delta 0.01 digits 7 range 0.0..99999.99;
   type Automobile is tagged private;
   procedure Print(Item : Automobile);
   function Set return Automobile;
   function Get_Price(Item : Automobile) return Prices;
   function Get_Company(Item : Automobile) return String;
   function Get_Model(Item : Automobile) return String;

   type Inventory is tagged private;
   procedure Read(Item : out Inventory; File : File_Type) with
     Pre => Mode(File) = In_File;

   procedure Write(Item : in Inventory; File : File_type) with
     Pre => Mode(File) = Out_File;

   procedure Print(Item : Inventory);
private
   type Automobile is tagged record
      Price   : Prices := 0.0;
      Company : Unbounded_String := Null_Unbounded_String;
      Model   : Unbounded_String := Null_Unbounded_String;
   end record;

   package Auto_Vect is new 
     Ada.Containers.Vectors(Index_Type   => Positive,
                            Element_Type => Automobile);
   use Auto_Vect;
   type Inventory is tagged record
      List : Vector;
   end record;
end Auto_Inventory;

此套餐的正文是:

with Ada.Strings.Fixed; use Ada.Strings.Fixed;

package body Auto_Inventory is
   package Prices_IO is new Ada.Text_IO.Decimal_IO(Prices);
   use Prices_IO;
   -----------
   -- Print --
   -----------

   procedure Print (Item : Automobile) is
      use Prices_Io;
   begin
      Put_Line("Price  : " & Prices'Image(Item.Price));
      Put_Line("Company: " & To_string(Item.Company));
      Put_Line("Model  : " & To_String(Item.Model));
      New_Line;
   end Print;

   ---------
   -- Set --
   ---------

   function Set return Automobile is
      Temp     : Automobile;
      Inpt_Str : String(1..1024);
      Length   : Natural;
   begin
      Put("Enter the automobile price: ");
      Get(Item => Temp.Price);
      Put("Enter the automobile company: ");
      Get_Line(Item => Inpt_Str, Last => Length);
      Temp.Company := To_Unbounded_String(Inpt_Str(1..Length));
      Put("Enter the automobile model: ");
      Get_Line(Item => Inpt_Str, Last => Length);
      Temp.Model := To_Unbounded_String(Inpt_Str(1..Length));
      return Temp;
   end Set;

   ---------------
   -- Get_Price --
   ---------------

   function Get_Price (Item : Automobile) return Prices is
   begin
      return Item.Price;
   end Get_Price;

   -----------------
   -- Get_Company --
   -----------------

   function Get_Company (Item : Automobile) return String is
   begin
      return To_String(Item.Company);
   end Get_Company;

   ---------------
   -- Get_Model --
   ---------------

   function Get_Model (Item : Automobile) return String is
   begin
      return To_String(Item.Model);
   end Get_Model;

   ----------
   -- Read --
   ----------

   procedure Read (Item : out Inventory;
                   File : File_Type)   is
      Temp : Inventory;
      Auto : Automobile;
      Inpt_Str : String(1..1024);
      Length   : Natural;
      Start, Finis : Positive;
   begin
      while not End_Of_File(File) loop
         Get(File => File, Item => Auto.Price);
         Get_Line(File => File, Item => Inpt_str, Last => Length);
         Start := Index_Non_Blank(Inpt_Str(1..Length));
         Finis := Start;
         while Finis < Length and then Inpt_Str(Finis) /= ' ' loop
            Finis := Finis + 1;
         end loop;
         Auto.Company := To_Unbounded_String(Inpt_Str(Start..Finis - 1));
         Start := Index_Non_Blank(Inpt_Str(Finis..Length));
         Auto.Model := To_Unbounded_String(Inpt_Str(Start..Length));
         Temp.List.Append(Auto);
      end loop;
      Item := Temp;
   end Read;

   -----------
   -- Write --
   -----------

   procedure Write (Item : in Inventory;
                    File : File_type) is

   begin
      for Element of Item.List loop
         Put(File => File, Item => Prices'Image(Element.Price) &
               " " & To_String(Element.Company) & " " & 
               To_String(Element.Model));
         New_Line;
      end loop;

   end Write;

   -----------
   -- Print --
   -----------

   procedure Print (Item : Inventory) is
   begin
      for Element of Item.List loop
         Element.Print;
      end loop;
   end Print;

end Auto_Inventory;

执行此程序包的主要过程示例:

------------------------------------------------------------------
-- Read a file of many records --
------------------------------------------------------------------
with Auto_Inventory; use Auto_Inventory;
with Ada.Text_IO; use Ada.Text_IO;

procedure read_file is
   The_Inventory : Inventory;
   The_File  : File_Type;
   File_Name : String := "inventory.txt";
begin
   Open(File => The_File,
        Mode => In_File,
        Name => File_Name);
   The_Inventory.Read(The_File);
   Close(The_File);
   The_Inventory.Print;
end read_file;

该程序的示例输入文件是:

22134.09 Kia Bernice
12201.15 Nissan Versa
22349.99 Chevrolet Cruse

答案 1 :(得分:-3)

我不清楚你使用的语言是什么。但是这个概念是单独处理文件中的每一行,然后根据你使用的语言处理令牌或分裂的函数处理它并保存变量中的每个标记取决于您使用的函数将如何保存标记

例如:

在java中有一个类

StringTokenizer(String str, String delim)

     StringTokenizer st = new StringTokenizer("this is a test", "$a; ");
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }
在你的情况下,

delim是空格,所以使用格式

StringTokenizer st = new StringTokenizer("this is a test");

    String line = reader.readLine();

  String[] tokens = line.split("\\s"); 

请注意,您需要将读取的行保存在字符串中,以便在java中使用这些函数,然后从数组中访问每个令牌

 String price =  tokens[1]  and so on 

对于其他语言,请找到以下资源:

在c https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm

在pyhton https://www.tutorialspoint.com/python/string_split.htm