读取包含“ [”的文件时出现CONSTRAINT_ERROR [英] CONSTRAINT_ERROR on reading a file containing "["

查看:125
本文介绍了读取包含“ [”的文件时出现CONSTRAINT_ERROR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读一个简单的文本文件。一切正常,但遇到方括号( [)字符时除外。然后我得到一个CONSTRAINT_ERROR。

I am reading a simple text file. Everything works as it should, except when an open bracket ("[") character is encountered. Then I get a 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;

在 s-wchcnv.adb:207中引发CONSTRAINT_ERROR。当WCEM_Brackets =>时,文件的相关部分是

The CONSTRAINT_ERROR is raised in "s-wchcnv.adb:207". The relevant part of the file is this

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.






如何关闭此功能,以便 [


How do I turn this off, so that "[" aren't being interpreted as WCEM?

编译器版本是GNAT GPL 2014(20140331)
操作系统是Windows 7 x64

Compiler version is GNAT GPL 2014 (20140331) Operating system is Windows 7 x64

再现CONSTRAINT_ERROR的小程序:

Small program that reproduces the 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;

Data.txt文件必须位于可执行文件附近。

A "Data.txt" file needs to be near the executable.

如果 Data.txt包含以下文本,则没问题。

If "Data.txt" contains text as below, no problem.

Hello
abc

添加 [,然后引发CONSTRAINT_ERROR:

Add "[" and a CONSTRAINT_ERROR is raised:

Hello[
abc


推荐答案

在Open调用中使用 Form参数,以将字符编码指定为 WCEM = b以外的括号。

Use the "Form" parameter to the Open call to specify the character encoding as something other than "WCEM=b" for bracket.

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;

有关文档,请参见此处

基思·汤普森(Keith Thompson)值得一提的是寻找相关文件。

Keith Thompson deserves the credit for finding the relevant documentation.

这篇关于读取包含“ [”的文件时出现CONSTRAINT_ERROR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆