# $readmemh -- is there something better?

**URL:** <https://verificationacademy.com/forums/t/readmemh-is-there-something-better/37933>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, readmemh, fgets, file-parsing\
**Created:** [August 11, 2020, 1:52am UTC](https://verificationacademy.com/forums/t/readmemh-is-there-something-better/37933 "2020-08-11T01:52:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![aale](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c89c15/32.png) [@aale](https://verificationacademy.com/forums/u/aale)\
**Post date:** [August 11, 2020, 5:22pm UTC](https://verificationacademy.com/forums/t/readmemh-is-there-something-better/37933/3 "2020-08-11T17:22:53Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/readmemh-is-there-something-better/37933/2):_

This almost worked. With $fscanf() as a while loop argument, the while loop hung. This tweak worked…

```verilog
typedef bit [15:0] uint16_t;
  typedef uint16_t uint16q_t[$];
  uint16q_t gain_offset_queue; 

  function automatic uint16q_t get_file_lines(string file_name);
      int fd;
      uint16_t value;
      uint16q_t ret;
      int line_num = 0;

      fd = $fopen(file_name, "r");
      if (fd) begin
          $display("File %s was opened successfully", file_name);
          while (!$feof(fd)) begin
              $fscanf(fd, "%x", value);
              ret.push_back(value);
              if (line_num < 10) begin
                $display ("Line[%04d]: 0x%04X", line_num++, ret[ret.size()-1]);
              end
          end
      end else begin     
          $display("File %s was NOT opened successfully", file_name);
      end

      $fclose(fd);
      return ret;
  endfunction : get_file_lines

```

Any idea why $fscanf as a while loop argument didn’t work?

Another interesting side note. Check out the $dsiplay and note the capital X in 0x%04X. The output looks like this:

# File ../../tb/raw\_proc/c\_model/output/grid.mem was opened successfully

# Line[0000]: 0x00ca

# Line[0001]: 0x00ac

# Line[0002]: 0x00dc

# Line[0003]: 0x0108

# Line[0004]: 0x010a

# Line[0005]: 0x0136

# Line[0006]: 0x015e

# Line[0007]: 0x0174

# Line[0008]: 0x0146

# Line[0009]: 0x015c

Why are the a-f characters not capitalized? I am using questa 10.7c.

Thanks!  
Aaron

---

_[View the full topic](https://verificationacademy.com/forums/t/readmemh-is-there-something-better/37933)._
