如何从 VHDL 中的文件变量中取回文件名字符串?
How to get back filename string from file variable in VHDL?
我想制作一个关闭并再次打开文件的 VHDL 程序。尽管该过程还会执行其他操作,但此操作实质上会将文件倒回到开头。
但是,我没有找到从文件句柄返回文件名的方法。
例如:
process
procedure close_and_open(file F : text) is
begin
file_close(F);
file_open(F, "HERE_LIES_THE_PROBLEM", read_mode);
end procedure
file Fi : text;
begin
file_open(Fi, "example_file.txt", read_mode);
close_and_open(Fi);
wait;
end process;
在这种情况下,Fi'simple_name、instance_name和path_name似乎只指名称“Fi”,而不是文件本身的名称。自然地,可以将文件名作为第二个参数传递,但在这种情况下它不会很直接(文件名具有 运行 时间生成的元素)。
您的示例中的 'file handle' 句柄是字符串文字。
IEEE Std 1076-2008 5.5.2 文件操作取决于 IEEE Std 1003.1-2004 便携式操作系统接口 (POSIX) - 请参阅附件 J。没有 1003.1 POSIX 恢复方式来自文件描述符的路径名(在 VHDL 中由 FILE 对象表示)。
在您的代码中,当由文字字符串表达式提供时,无法从文件声明中恢复文件逻辑名或从 file_open 过程中恢复 external_name。没有要评估的命名对象。
您可以在 运行 时操作字符串表达式,方法是将它的值分配给访问类型为字符串类型的对象。这提供了一个表示字符串对象的对象名称。
由于访问类型对象的变量 class,将文件名参数留在过程调用之外是范围和可见性声明顺序练习。表示对象的值由带有后缀 all 的选定名称访问(8.3 选定名称第 5 段)。
构建一个Minimal Complete and Verifiable example:
use std.textio.all;
entity close_open is
end entity;
architecture foo of close_open is
begin
NOTLABELED:
process
type filename is access string; -- ADDED
variable file_name: filename; -- ADDED
procedure close_and_open(file F : text) is
begin -- ADDED
file_close(F);
-- file_open(F, "HERE_LIES_THE_PROBLEM", read_mode);
file_open(F, file_name.all, read_mode); -- CHANGED
end procedure;
file Fi : text;
begin
file_name := new string'("example_file.txt"); -- ADDED
-- file_open(Fi, "example_file.txt", read_mode);
file_open(Fi, file_name.all, read_mode); -- CHANGED
close_and_open(Fi);
wait; -- ADDED
end process;
end architecture;
访问类型声明、访问类型变量声明和过程体之间存在声明顺序依赖关系。
本例分析、阐述和运行s(要求file_name能够打开读取)。
添加了等待语句,防止进程不停地打开和关闭文件。您的程序正文中也缺少开头。
我想制作一个关闭并再次打开文件的 VHDL 程序。尽管该过程还会执行其他操作,但此操作实质上会将文件倒回到开头。
但是,我没有找到从文件句柄返回文件名的方法。
例如:
process
procedure close_and_open(file F : text) is
begin
file_close(F);
file_open(F, "HERE_LIES_THE_PROBLEM", read_mode);
end procedure
file Fi : text;
begin
file_open(Fi, "example_file.txt", read_mode);
close_and_open(Fi);
wait;
end process;
在这种情况下,Fi'simple_name、instance_name和path_name似乎只指名称“Fi”,而不是文件本身的名称。自然地,可以将文件名作为第二个参数传递,但在这种情况下它不会很直接(文件名具有 运行 时间生成的元素)。
您的示例中的 'file handle' 句柄是字符串文字。
IEEE Std 1076-2008 5.5.2 文件操作取决于 IEEE Std 1003.1-2004 便携式操作系统接口 (POSIX) - 请参阅附件 J。没有 1003.1 POSIX 恢复方式来自文件描述符的路径名(在 VHDL 中由 FILE 对象表示)。
在您的代码中,当由文字字符串表达式提供时,无法从文件声明中恢复文件逻辑名或从 file_open 过程中恢复 external_name。没有要评估的命名对象。
您可以在 运行 时操作字符串表达式,方法是将它的值分配给访问类型为字符串类型的对象。这提供了一个表示字符串对象的对象名称。
由于访问类型对象的变量 class,将文件名参数留在过程调用之外是范围和可见性声明顺序练习。表示对象的值由带有后缀 all 的选定名称访问(8.3 选定名称第 5 段)。
构建一个Minimal Complete and Verifiable example:
use std.textio.all;
entity close_open is
end entity;
architecture foo of close_open is
begin
NOTLABELED:
process
type filename is access string; -- ADDED
variable file_name: filename; -- ADDED
procedure close_and_open(file F : text) is
begin -- ADDED
file_close(F);
-- file_open(F, "HERE_LIES_THE_PROBLEM", read_mode);
file_open(F, file_name.all, read_mode); -- CHANGED
end procedure;
file Fi : text;
begin
file_name := new string'("example_file.txt"); -- ADDED
-- file_open(Fi, "example_file.txt", read_mode);
file_open(Fi, file_name.all, read_mode); -- CHANGED
close_and_open(Fi);
wait; -- ADDED
end process;
end architecture;
访问类型声明、访问类型变量声明和过程体之间存在声明顺序依赖关系。
本例分析、阐述和运行s(要求file_name能够打开读取)。
添加了等待语句,防止进程不停地打开和关闭文件。您的程序正文中也缺少开头。