ADA编程如何不覆盖input/output中的数据

How to not overwritten the data in input/output in ADA programming

我需要获取用户的输入并将数据保存在文件中,但数据不能被覆盖。代码 运行 很好,但数据被覆盖了。如何更正我的代码以获得期望的结果?

我无法覆盖数据。需求为:"Ask the user to input name, id and CGPA one by one, and then write it in the file. Existing data must not be "。我也试过将新生成的文件数据存储在另一个文件中,但得到了相同的结果。

with Ada.Command_Line, Ada.Text_IO;
use Ada.Command_Line, Ada.Text_IO;
procedure Main is
   -- Initializing
   Read_From     : constant String := "inputFile.txt";
   Write_To      : constant String := "studentData.txt";
   name          : String (1 .. 13);
   studentID     : String (1 .. 11);
   cgpa          : String (1 .. 4);
   Input, Output : File_Type;
begin
   -- taking inputs
   Put_Line ("My Student ID Is *******bc150400162*******");
   Put_Line ("Enter the details for first student.");
   Put_Line ("Please enter your name:");
   Get (name);
   Put_Line ("Please enter your Student ID:");
   Get (studentID);
   Put_Line ("Please enter your CGPA:");
   Get (cgpa);
   -- opening file
   begin
      Open (File => Input, Mode => In_File, Name => Read_From);
   exception
      when others =>
         Put_Line
           (Standard_Error,
            "Can not open the file '" & Read_From & "'. Does it exist?");
         Set_Exit_Status (Failure);
         return;
   end;
   -- Creating new file file

   begin
      Create (File => Output, Mode => Out_File, Name => Write_To);
   exception
      when others =>
         Put_Line
           (Standard_Error, "Can not create a file named '" & Write_To & "'.");
         Set_Exit_Status (Failure);
         return;
   end;
   -- Here is the loop.............................................
   ------------------
   loop
      declare
         Line : String := Get_Line (Input);
      begin
         Put_Line (Output, Line);
         Put_Line (Output, "The Student details are: ");
         Put_Line (Output, name);
         Put_Line (Output, studentID);
         Put_Line (Output, cgpa);
      end;
   end loop;
exception
   when End_Error =>
      if Is_Open (Input) then
         Close (Input);
      end if;
      if Is_Open (Output) then
         Close (Output);
      end if;
end Main;

执行此操作的标准方法是尝试以追加模式打开文件,如果失败则创建它(再次以追加模式)。如果创建文件失败,你有一个不同的问题(例如非法名称?没有权限?文件系统是read-only?文件系统已满?none这些在你的程序中可寻址!)

注意,先打开,打开失败再创建;反过来,文件可能会被重置,这正是你不想要的。

with Ada.Text_IO;
with Ada.Calendar.Formatting;
with Ada.IO_Exceptions;
procedure Appending is
   Output : Ada.Text_IO.File_Type;
   Name : constant String := "appending.dat";
begin

我们这里需要一个块,所以可以在这里捕获异常。

   begin

尝试打开文件...

      Ada.Text_IO.Open (File => Output,
                        Name => Name,
                        Mode => Ada.Text_IO.Append_File);

Open成功!

   exception
      when Ada.IO_Exceptions.Name_Error =>

Open 失败,因为该名称不代表可打开的文件。尝试创建它 ...

         Ada.Text_IO.Create (File => Output,
                             Name => Name,
                             Mode => Ada.Text_IO.Append_File);

文件 Output 现已打开,处于追加模式。
(乍一看,您可能想知道在追加模式下打开一个必须为空的文件有什么意义。通常,当然,它也可能在标准输出模式下打开;唯一的区别是如果对于某些你必须使用无模式的原因 Reset。在那种情况下,如果文件是在追加模式下创建的,它将保持在追加模式下,因此任何以前的更新都不会丢失。)

   end;

给它写点东西'unique',这样我们就可以知道它有效...

   Ada.Text_IO.Put_Line
     (File => Output,
      Item => Ada.Calendar.Formatting.Image (Ada.Calendar.Clock));

...我们完成了。可以保留 OS 以在退出时为我们关闭文件,但请确定。

   Ada.Text_IO.Close (File => Output);
end Appending;