将文件的字符串长度与文件的整数匹配

Matching a file's string length with a file's integer

我一直在处理文件分配并遇到了一个棘手的问题,我需要读取文件的编号并通过比较第一个整数和第二个整数来匹配它们,用 space 分隔。如果后者的数量高于前者的成分数量,我应该将它们打印出来。

      Create(F, Out_File, File_Name);
      Put_Line(F, "23709 5");
      Put_Line(F, "001 4");
      Put_Line(F, "1 1");
      Put_Line(F, "10923487283462 14");
      Put_Line(F, "22 3");

在上面的场景中,这是真正的问题,我想打印行

001 4
22 3

因为它们不匹配。

最初我计划通过添加一个 Check 变量来解决这个问题,该变量等于 10 ** 校验和(上面的后一个整数)-1。这会起作用,除非它不是第四个数字太大而无法读取,所以我选择沿着使第一个整数成为字符串并尝试将其长度与校验和整数相匹配的路径。这是我 运行 遇到问题的地方。不幸的是,我命名字符串的方式存储了前几行的数字,并以此为基础。而且因为它们相互构建,所以我的检查变量不起作用,因为它与字符串的实际长度不匹配,而是与字符串的组合长度匹配,然后是一些。

这是我的代码:

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;

-----------------------------------------------------------------------
-- Im supposed to read the following file and detect
-- whether or not the integer in the file following immediately after
-- the space indicates how many integer places are found
-- in the integer infront of the space. If it mismatches
-- Im supposed to print it.
----------------------------------------------------------------------
procedure exercise2 is

   F: File_Type;
   File_Name: constant String := "BAD_DIGITS.TXT";
   Space : Character;
   Checksum: Integer;
   Name : String(1..20);
   L: Integer := 0;
   Ints : Integer;
   Char : Character;

   begin

      Create(F, Out_File, File_Name);
      Put_Line(F, "23709 5");
      Put_Line(F, "001 4");
      Put_Line(F, "1 1");
      Put_Line(F, "10923487283462 14");
      Put_Line(F, "22 3");

      Close(F);

      Open(F, In_File, File_Name);

      while not End_Of_File(F) loop
     while not End_Of_Line(F) loop
        loop
           Get(F, Char);
           L := L+1;
           if Char = ' ' then
          exit;
           else
          Name(L) := Char;
           end if;
        end loop;
        Get(F, Checksum);
        if L-1 > Checksum then
           Put(Name);
           Put(" ");
           Put(Checksum, Width =>0);
           New_Line;
           L := 0;
        end if;
     end loop;
     Skip_Line(F);
      end loop;




end exercise2;

我整天都在为这个问题坐着,我希望有人能提供一些建设性的反馈来解放我的思绪。

确保在阅读每一行之前将字符计数器归零。 只有当角色不是 space 时才增加你的角色计数器。 以整数形式读取校验和值。

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   F: File_Type;
   File_Name: constant String := "BAD_DIGITS.TXT";
   Space : constant Character :=  ' ';
   Checksum: Integer;
   Value : String(1..20);
   L: Integer := 0;
   Char : Character;
begin
   Create(F, Out_File, File_Name);
   Put_Line(F, "23709 5");
   Put_Line(F, "001 4");
   Put_Line(F, "1 1");
   Put_Line(F, "10923487283462 14");
   Put_Line(F, "22 3");

   Close(F);

   Open(F, In_File, File_Name);

   while not End_Of_File(F) loop
      L := 0;
      loop
         Get(File => F,
             Item => Char);
         if Char /= Space then
            L := L + 1;
            Value(L) := Char;
         else
            exit;
         end if;
      end loop;
      Get(F, Checksum);  -- Uses Ada.Integer_Text_Io
      if Checksum /= L then
         Put_Line(Value(1..L) & Checksum'Image);
      end if;

   end loop;
 end main;