在VHDL中,如何在打开文件之前检查文件是否存在?

在VHDL中,如何在打开文件之前检查文件是否存在?,vhdl,Vhdl,在Verilog中,我可以通过打开文件然后检查文件描述符是否为零来检查文件是否存在,如果不存在,则假定文件不存在。例如: module testbench; function file_exists; input [80*8:0] filename; integer file; integer error; begin file = $fopen(filename

在Verilog中,我可以通过打开文件然后检查文件描述符是否为零来检查文件是否存在,如果不存在,则假定文件不存在。例如:

    module testbench;

    function file_exists;
        input [80*8:0] filename;        
        integer        file;
        integer        error;
    begin
        file = $fopen(filename, "r");
        if (!file) begin
           $display("\nFile Open Failed with Error Code = %x", error); 
           file_exists = 0;
        end
        else begin
           $fclose(file);
           file_exists = 1;
        end
    end
    endfunction

    integer x;
    initial begin
       x = file_exists("sdfsdf.txt");
       $display("x: %0b", x);          
    end

    endmodule

如何在vhdl中执行相同的操作?

打开文件时,例如:

file_open(status, file_handle, “my_file.txt”, read_mode);

您得到的状态类型为
文件\u打开\u状态
。它可以有许多值:
open\u ok
status\u error
name\u error
mode\u error
。如果在任何语言中都找不到该文件,则会出现
name\u错误

在打开文件之前,请检查文件是否存在。你试图打开它。如果您的尝试失败,请检查失败的原因,如果失败是由不存在引起的,则您在尝试打开文件时知道它不存在。TCL可以在打开文件之前检查文件是否存在,我相信其他许多人也可以。IEEE Std 1076-2008 5.5.2文件操作“在第二种形式的文件_OPEN中,通过Status参数返回的值表示过程调用的结果:“…”NAME_ERROR的值表示外部文件不存在(在尝试从外部文件读取的情况下)“…..VHDL-2019将添加一个
dir_ItemExists(…)
为此目的的步骤。有关详细信息,请参阅。
use ieee.std_logic_1164.all;
use std.textio.all;          

entity testebench is 
end entity;

architecture sim of testbench is 

    impure function file_exists(
        filename : in string
    ) return boolean is
        variable open_status :FILE_OPEN_STATUS;
        file     infile      :text;
    begin
        file_open(open_status, infile, filename, read_mode);
        if open_status /= open_ok then
            return false;
        else
            file_close(infile);
            return true;
        end if;
    end function;

begin

    process
        f1 :boolean;
    begin
        f1 = file_exists("fgsfgsdfg.txt")
        report "found: " & boolean'image(f1);
    end process;

end architecture;