How to read a file in systemVerilog and assign its values?

I have a file with multiple lines with the following structere

r,ACFE13D5,00000000

w,FE1234AC,00000000

The mid value is hexa adreess, and the last value should be data with lengh of 32 bit(also hexa).

What is the simple way ro read such a file and get the values into the following transaction prototype:

      direction_enum   rw;  // where-      typedef enum { READ = 0, WRITE = 1 }                          direction_enum;
    
       bit [31:0] addr; 
       bit [31:0] data;

In reply to saritr:

I think, SV does not have any system function to read a file with Mixed data type values.(e.g. int, hex, hex).

If you consider implementing file with only Hex or Binary values, then these are the functions.

$readmemb("file", variable_array[]); // To read binary values from file
$readmemh("file", variable_array[]); // To read hex values from file

*In reply to mayurkubavat:*Not correct. Use $fscanf

int fd; // file descriptor
byte mode;
int status;
fd = $fopen("filename","r");
status = $fscanf("%c,%h,%h",mode,addr,data); 
if(status != 3) error;// you did not read in 3 values
case(mode)
"r": rw=READ;
"w": rw=WRITE;
default: error;
endcase

In reply to dave_59:

Awesome! Thanks for pointing it out.

In reply to dave_59:

Don’t you have to define error?

In reply to saritr:

I don’t have to, but you do :)

I’m only showing you how to do the file I/O. It’s up to you to write the code around it.