Ada CONSTRAINT“读取包含”的文件时出错;[quot;
我正在阅读一个简单的文本文件。除了遇到一个开括号(“[”)字符外,一切都正常工作。然后我得到一个约束错误 我的职能是:Ada CONSTRAINT“读取包含”的文件时出错;[quot;,ada,gnat,Ada,Gnat,我正在阅读一个简单的文本文件。除了遇到一个开括号(“[”)字符外,一切都正常工作。然后我得到一个约束错误 我的职能是: ---------------------------------------------- -- GET_FILE_CONTENTS function Get_File_Contents (File_Name : in String) return String_Array is -- Loads the entire file into a dynamica
----------------------------------------------
-- 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”中提出。文件的相关部分如下所示
when WCEM_Brackets =>
if C /= '[' then
return Character'Pos (C);
end if;
if In_Char /= '"' then
raise Constraint_Error; <======= CONSTRAINT_ERROR
end if;
如何关闭此选项,以便“[”不会被解释为WCEM 编辑: 编译器版本为GNAT GPL 2014(20140331) 操作系统是Windows7x64 复制约束错误的小程序:
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”文件需要靠近可执行文件
如果“Data.txt”包含如下文本,则没有问题
Hello
abc
添加“[”并引发约束错误:
Hello[
abc
在Open调用中使用“Form”参数指定字符编码,而不是括号中的“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;
有关相关文档,请参阅
Keith Thompson找到相关文档值得称赞。使用Open调用的“Form”参数指定字符编码,而不是括号中的“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;
有关相关文档,请参阅
Keith Thompson找到相关文档值得表扬。鉴于这个问题的性质,我建议在这个问题中添加OS和Gnat编译器版本。如果你能将代码编译成一个完整的可编译版本(一个最小的主程序和一个失败的单行文字文件),这可能也很有用。这不会解决您的问题,但因为您正在从文件中读取无界字符串:Ada有一些包可以直接支持这一点,因此您的代码不需要与无界字符串进行所有转换。请查看
Ada.Text\u IO.unbounded\u IO
,Ada.Wide\u Text\u IO.unbounded\u IO
和Ada.Wide>_Wide_Text_IO.Unbounded_IO
@BrianDrummond我采纳了你的建议并包含了所需的信息。@egilhh非常感谢,我不知道。@KeithThompson你不小心切断了链接中的最后一个数字,该数字链接到了另一个错误。鉴于此问题的性质,我建议添加操作系统d Gnat编译器版本的问题。如果你能把代码编译成一个完整的可编译版本(一个最小的主程序,一个失败的单行文本文件),这可能也很有用。这不会解决您的问题,但因为您正在从文件中读取无界字符串:Ada有一些包可以直接支持这一点,因此您的代码不需要与无界字符串进行所有转换。请查看Ada.Text\u IO.unbounded\u IO
,Ada.Wide\u Text\u IO.unbounded\u IO
和Ada.Wide>_Wide_Text_IO.Unbounded_IO
@BrianDrummond我接受了你的建议并包含了所需的信息。@egilhh非常感谢,我不知道。@KeithThompson你不小心切断了链接的最后一个号码,该号码链接到了另一个bug。你之前发布的是