生成Constraint_Error上读取包含“[”文件(CONSTRAINT_ERROR on r

2019-10-21 20:56发布

我读一个简单的文本文件。 一切正常,因为它应该是遇到开括号(“[”)字符时除外。 然后,我得到一个生成Constraint_Error。

我的功能是:

----------------------------------------------
-- GET_FILE_CONTENTS
function Get_File_Contents (File_Name : in String)
    return String_Array is
    -- Loads the entire file into a dynamically sized
    -- array of Unbounded_Wide_String.

    -- The line count is used to dynamically size the array.
    Line_Count : Natural
               := 0;

    File : Ada.Wide_Text_IO.File_Type;
begin
    -- Get the line count before opening the file.
    Line_Count := Get_File_Line_Count (File_Name);

    Ada.Wide_Text_IO.Open (File,
                           In_File,
                           File_Name);
    declare
        Lines : String_Array (1 .. Line_Count);
    begin

        -- Step through the file and save each line.
        for Current_Line in reverse 1 .. Line_Count loop
            Lines(Current_Line) := To_Unbounded_Wide_String (Ada.Wide_Text_IO.Get_Line (File));
        end loop;

    -- Remember to close the file.
    Ada.Wide_Text_IO.Close (File);
    return Lines;

end;
end Get_File_Contents;

该生成Constraint_Error在 “:207 S-wchcnv.adb” 提出的。 该文件的相关部分是这样的

when WCEM_Brackets =>
    if C /= '[' then
       return Character'Pos (C);
    end if;

    if In_Char /= '"' then
       raise Constraint_Error; <======= CONSTRAINT_ERROR
    end if;

这是我找到的信息:

--  Note on the use of brackets encoding (WCEM_Brackets). The brackets
--  encoding method is ambiguous in the context of this function, since
--  there is no way to tell if ["1234"] is eight unencoded characters or
--  one encoded character. In the context of Ada sources, any sequence
--  starting [" must be the start of an encoding (since that sequence is
--  not valid in Ada source otherwise). The routines in this package use
--  the same approach. If the input string contains the sequence [" then
--  this is assumed to be the start of a brackets encoding sequence, and
--  if it does not match the syntax, an error is raised.

如何关闭这个功能,使“[”不被解释为WCEM?

编辑:

编译器的版本是GNAT GPL 2014(20140331),操作系统为Windows 7 64位系统

小程序,再现生成Constraint_Error:

with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Main is
    File : File_Type;
    File_Name : String := "Data.txt";
begin
    Open (File,
          In_File,
          File_Name);

    while not End_Of_File (File) loop
    declare
        Line : Wide_String := Get_Line (File);
    begin
        Put_Line (Line);
    end;
    end loop;

    Close (File);
end Main;

A“DATA.TXT”文件需要可执行附近。

如果“DATA.TXT”包含如下文字,没有任何问题。

Hello
abc

添加“[”和生成Constraint_Error提高:

Hello[
abc

Answer 1:

使用“表”参数来打开调用指定的字符编码为比“WCEM = B”的支架以外的东西。

with Ada.Wide_Text_IO; use Ada.Wide_Text_IO;

procedure Main is
    File : File_Type;
    File_Name : String := "Data.txt";
begin
    Open (File,
          In_File,
          File_Name, 
          Form => "WCEM=8");

    while not End_Of_File (File) loop
    declare
        Line : Wide_String := Get_Line (File);
    begin
        Put_Line (Line);
    end;
    end loop;

    Close (File);
end Main;

对于有关文件,看这里

基思·汤普森值得查找相关文件的功劳。



文章来源: CONSTRAINT_ERROR on reading a file containing “[”
标签: ada gnat