File writing

I have a module that writes two hexadecimal values to a text file say “abc.txt”. I am using the $fdisplay(edit 1: tried $fwrite as well) construct in SV and have two print statements for debugging above and below the $fdisplay.

Upon running, the log has debug prints, however, the file abc.txt is empty

Sharing the code snippet

always @ (posedge clk)
  begin
    $display("file write debug outside if");
    if(dut_wvalid)
      begin
        $display("file write debug inside if");
        $fwrite(fd1,"%h %h",awaddr,wdata);
        $display("write executed above this");
      end
  end

Please show us your $fopen and $fclose statements.

In reply to sbellock:
The file opening happens in an initial block inside the same module.

In reply to avinash_nambiar:

What value does fd1 have after calling $fopen?

In reply to dave_59:

Hi Dave, fd1 takes up an integer value -2147483645.
Apparently, I got it working by changing the code to

always @ (posedge clk)
  begin
    if(dut_wvalid)
      begin
        fd1 = $fopen("axi_write.txt","a"); //Opening file for write
        $fwrite(fd1,"%h %h \n",awaddr,wdata);
        $fclose(fd1);
      end
  end

But would like to know why it didn’t work in the previous case

In reply to avinash_nambiar:

So would I.