# File writing

**URL:** <https://verificationacademy.com/forums/t/file-writing/36231>\
**Category:** SystemVerilog\
**Tags:** fileread, filewrite, SystemVerilog\
**Created:** [June 27, 2019, 1:40pm UTC](https://verificationacademy.com/forums/t/file-writing/36231 "2019-06-27T13:40:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![avinash\_nambiar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@avinash\_nambiar](https://verificationacademy.com/forums/u/avinash_nambiar)\
**Post date:** [June 27, 2019, 1:40pm UTC](https://verificationacademy.com/forums/t/file-writing/36231/1 "2019-06-27T13:40:39Z")

</div>

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

```verilog
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

```

---

<div class="post-metadata">

**Author:** ![sbellock](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sbellock](https://verificationacademy.com/forums/u/sbellock)\
**Post date:** [June 27, 2019, 3:33pm UTC](https://verificationacademy.com/forums/t/file-writing/36231/2 "2019-06-27T15:33:49Z")

</div>

Please show us your $fopen and $fclose statements.

---

<div class="post-metadata">

**Author:** ![avinash\_nambiar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@avinash\_nambiar](https://verificationacademy.com/forums/u/avinash_nambiar)\
**Post date:** [June 28, 2019, 4:22am UTC](https://verificationacademy.com/forums/t/file-writing/36231/3 "2019-06-28T04:22:49Z")

</div>

_In reply to [sbellock](https://verificationacademy.com/forums/t/file-writing/36231/2):_  
The file opening happens in an initial block inside the same module.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 28, 2019, 5:08am UTC](https://verificationacademy.com/forums/t/file-writing/36231/4 "2019-06-28T05:08:13Z")

</div>

_In reply to [avinash\_nambiar](https://verificationacademy.com/forums/t/file-writing/36231/3):_

What value does fd1 have after calling $fopen?

---

<div class="post-metadata">

**Author:** ![avinash\_nambiar](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/a88e4f/32.png) [@avinash\_nambiar](https://verificationacademy.com/forums/u/avinash_nambiar)\
**Post date:** [June 28, 2019, 5:11am UTC](https://verificationacademy.com/forums/t/file-writing/36231/5 "2019-06-28T05:11:06Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/file-writing/36231/4):_

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

```verilog
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

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 28, 2019, 3:49pm UTC](https://verificationacademy.com/forums/t/file-writing/36231/6 "2019-06-28T15:49:54Z")

</div>

_In reply to [avinash\_nambiar](https://verificationacademy.com/forums/t/file-writing/36231/5):_

So would I.
