Not able to open file in read mode

Hi,

I am trying to open a file and read it’s content in an array. The file handler is not getting populated throwing an error “file can not be opened. No such file or directory”. The file that I am trying to open is in the same directory. Could you please suggest anything to overcome this file read issue?

Here is the code snippet:

virtual task main_phase(uvm_phase phase);
integer i, file_handle;

super.main_phase(phase);
phase.raise_objection(this,“”);

file_handle = $fopen(“pattern.txt”, “r”);
if(file_handle != null)
for(i=0;i<250; i++) begin
return_val= $fread(mem[i] ,file_handle);
$display(“Data read from file is mem[%d] = %h”,i,mem[i]);
end

phase.drop_objection(this);
endtask: main_phase

Priyanka

You should try to write to a file and see if it gets created in the same directory that you are trying to read from. Also $fopen returns 0 if it is not able to open the file, not null. The ‘value’ of null is not defined.

1 Like

Hi,

try opening file with $fopenr(“File_name”).
e.g
integer file1;
bit return_val;
bit [7:0]mem[64:0];
initial
begin
file1= $fopenr(“pattern.txt”);
for( int i=0;i < 64; i++)
begin
return_val= $fread(mem[i] ,file1);
$display(“Data read from file is mem[%d] = %h”,i,mem[i]);
end
end
Be sure, pattern.txt is present in same folder.

Thanks
Kinjal
SiconTech

hello Kinjals,
when I use the $fopenr, it will report compile error, and I also didn’t find it in Standard for SystemVerilog

$fopenr was from an open-source PLI package used prior to Verilog-2001. See Verilog File I/0,Verilog file handling