如何将文件作为过程的参数传递

时间:2013-11-20 02:38:09

标签: readfile ada

我正在尝试编写一个子程序来读取“grocery.txt”文件。格式如下: 12 7.2橙色

第一列是整数,第二列是浮点数。第三个是字符串,直到行尾。我应该如何声明参数以及如何将文件变量作为子过程的参数传递?

 PROCEDURE Read_File(Ada.Text_IO.File_Type => in_Item,
  Item : out Grocery_Item) IS
 BEGIN
  Ada.Integer_Text_IO.Get(File => In_Item,
     Item => Grocery_Item.Quantity,
        Width => 3);
  Ada.Float_Text_IO.Get(File => In_Item, Item => Grocery_Item.Cost_Per_Item,
     Width => 3);
  Ada.Text_IO.Get_Line(Item => Grocery_Item.Item.Item_Name,
     Last => Grocery_Item.Item.Item_Name_Length);
 END Read_File;

我一直收到错误,说我在PROCEDURE专线上错过了“,”。

非常感谢。

1 个答案:

答案 0 :(得分:5)

=>仅在调用过程或函数时使用。声明它们时,语法为 parameter-name :[in / out / in out / access] type ,就像使用Item : out Grocery_Item一样:< / p>

procedure Read_File (In_Item : in Ada.Text_IO.File_Type;  -- the "in" keyword is optional
                     Item    : out Grocery_Item) is
...

(为了避免任何可能的混淆,=>也用在与程序或函数(子程序)无关的上下文中。重点是当你使用子程序时,你可以使用{{ 1}}当你打电话给他们时,但不是你宣布他们的时候。)

另外,我不确定哪个是类型,哪个是参数名称:=>Item?如果Grocery_Item是类型,则参数声明很好,但在正文中使用Grocery_ItemGrocery_Item.Quantity等没有意义,因为它不适合类型名称。也许你想说Grocery_Item.Item.Item_Name_Length等等我说不出来。如果Item.Quantity实际上是类型,并且Item将是参数的名称,那么您需要在过程的顶部切换它们:

Grocery_Item

但是要确定,我们必须看看procedure Read_File (In_Item : in Ada.Text_IO.File_Type; -- the "in" keyword is optional Grocery_Item : out Item) is ... type Item的声明是什么。

最后,另外一件事:为什么前两个type Grocery_Item调用使用文件,但最后一个Get没有?这将导致文件中的两次读取,第三次读取将来自键盘(可能)。您可能还希望将Get_Line参数添加到上一个File