从 Ada 中的文件中读取记录?

发布于 2024-12-18 06:24:32 字数 1041 浏览 1 评论 0原文

每当我从 Ada 中的文件中读取记录时,我总是会收到错误。该程序的目标是(从文件中)读取一个整数,该整数是需要记录的项目数,姓氏由(最多)12 个字母组成,名字由(最多)12 个字母组成,和一个浮点值,然后将它们存储到记录中。

这是来自 AdaGIDE:

record2.adb:32:04:调用中的无效参数列表(使用 -gnatf 了解详细信息)

我的代码:


with Ada.Text_IO, Ada.float_Text_IO, ada.Integer_Text_IO;
use Ada.Text_IO, Ada.float_Text_IO, ada.Integer_Text_IO;

procedure Record2 is

   TYPE Testrec IS
      record
         test1 : string (1..12);
         test2 : string (1..12);
         test3 : float;
   END RECORD;

   T:  Testrec;   
   Lt: Integer; 
   numitem: integer;

   random1:   Ada.Text_IO.File_Type;

begin -- Record2

   Ada.Text_IO.Open(File => random1, Mode => Ada.Text_IO.In_File, Name => "info1.dat");

   Get_Line(File => random1, Item => Testrec, Last => Lt);

   Put(T.test1); 
   Put(T.Test2);
   Put(T.Test3);

end Record2;

info1.dat 的内容(没有额外的空格或行,只是从“L”到“0”:

LastName    FirstName   4.00

我的问题是 Get_Line,我知道 LastName 填充了 12 个字符,FirstName 也是如此,那么我到底做错了什么?

Whenever I go to read in record from a file in Ada, I always get an error. The goal of the program is to read (from a file) an integer which is how many items needed to be recorded, in a last name consisting of (at most) 12 letters, a first name consisting of (at most) 12 letters, and a float value, then store those into a record.

This was from AdaGIDE:

record2.adb:32:04: invalid parameter list in call (use -gnatf for details)

My code:


with Ada.Text_IO, Ada.float_Text_IO, ada.Integer_Text_IO;
use Ada.Text_IO, Ada.float_Text_IO, ada.Integer_Text_IO;

procedure Record2 is

   TYPE Testrec IS
      record
         test1 : string (1..12);
         test2 : string (1..12);
         test3 : float;
   END RECORD;

   T:  Testrec;   
   Lt: Integer; 
   numitem: integer;

   random1:   Ada.Text_IO.File_Type;

begin -- Record2

   Ada.Text_IO.Open(File => random1, Mode => Ada.Text_IO.In_File, Name => "info1.dat");

   Get_Line(File => random1, Item => Testrec, Last => Lt);

   Put(T.test1); 
   Put(T.Test2);
   Put(T.Test3);

end Record2;

info1.dat's contents (no extra spaces or lines, just from "L" to "0":

LastName    FirstName   4.00

My problems is the Get_Line, that I know. LastName is padded with spaces, filling the 12 characters, the same goes for FirstName. Then the float is taken for its value in general. What exactly am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-12-25 06:24:32

基本上,您使用读取字符串的Get_Line来尝试读取记录的实例。

由于这看起来像是一项家庭作业(没关系),我会给你一个提示:

尝试单独阅读这些字段。

这还不足以完全解决您的问题,但它会让您走得更远,从那时起您可以尝试解决其余问题。

Basically, you're using Get_Line, which reads strings, to attempt to read an instance of a record.

Since this looks like a homework assignment (which is okay), I'll give you a hint:

Try reading the fields individually.

That's not enough to totally solve your problem, but it'll get you further, from which point you can try to work out the rest.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文