file - 无法在 Ada 95 中创建文件

标签 file ada

我试图遵循打开文件的标准引用,但当我调用 Ada.Text_IO.Create() 时,在该行遇到了constraint_error。它说“范围检查失败”。感谢任何帮助,这是代码:

WITH Ada.Text_IO;
WITH Ada.Integer_Text_IO;
USE Ada.Text_IO;
USE Ada.Integer_Text_IO;

PROCEDURE FileManip IS

   --Variables
   Start_Int : Integer;
   Stop_Int : Integer;
   Max_Length : Integer;

   --Output File
   MaxName : CONSTANT Positive := 80;
   SUBTYPE NameRange IS Positive RANGE 1..MaxName;

   OutFileName : String(NameRange) := (OTHERS => '#');
   OutNameLength : NameRange;
   OutData : File_Type;

   --Array
   TYPE Chain_Array IS ARRAY(1..500) OF Integer;
   Sum : Integer := 1;
BEGIN

--Inputs
   Ada.Text_IO.Put(Item => "Enter a starting Integer: ");
   Ada.Integer_Text_IO.Get(Item => Start_Int);
   Ada.Text_IO.New_Line;
   Ada.Text_IO.Put(Item => "Enter a stopping Integer: ");
   Ada.Integer_Text_IO.Get(Item => Stop_Int);
   Ada.Text_IO.New_Line;
   Ada.Text_IO.Put(Item => "Enter a Maximum Length to search: ");
   Ada.Integer_Text_IO.Get(Item => Max_Length);
   Ada.Text_IO.New_Line;
   Ada.Text_IO.Put(Item => "Enter a output file name > ");
   Ada.Text_IO.Get_Line(
      Item => OutFileName,
      Last => OutNameLength);
   Ada.Text_IO.Create(
      File => OutData,
      Mode => Ada.Text_IO.Out_File,
      Name => OutFileName(1..OutNameLength));
   Ada.Text_IO.New_Line;

最佳答案

可能不是 create 给您带来了这个问题,而是对 OutFileName(1..OutNameLength) 进行了数组范围检查。

要发生这种情况,1 或 OutNameLength 需要超出为 OutFileName 指定的范围。由于它被定义为 RANGE 1..MaxName (通过 NameRange),我们知道它不是 1,所以罪魁祸首一定是 OutNameLength

环顾四周,您似乎从 Get_LineLast 参数中获取了该值。在一种情况下,您可以从该参数中获取 0:当您读取空行时。所以我的猜测就是正在发生的事情。

关于file - 无法在 Ada 95 中创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2430106/

相关文章:

python - 从文件中搜索字符串 - python

java - 程序将一个文件的数据复制到另一个文件15次

node.js - Cordova 3.5.0 FileTransfer + NodeJS(多部分/表单数据)上传问题

ada - 在 Ada 中将空枚举传递给泛型的惯用方法

ada - 从适用于 Linux 的 Windows 子系统调用时,GPRBuild 找不到 gprconfig

c++ - fstream 库,试图创建一个具有变量名的文件 (c++)

file - 如何优化从 Go 中的文本文件中查找字谜

ada - 如何读取每个单词并将其存储到变量中以供使用?

sockets - 使用 GNAT.Sockets 接收多播流量

c - C 中的 typedef 和 A​​da 中的应用程序定义类型之间的区别