How to read a csv and extract some data

My input file “in.csv” its contents are as below:

# this is input file
# 
FOR 2
ANT, 4
BCC, 0
FCC, 2
DATA, 0x600

ANT, 6
ANT, 8
ANT, 10

I want to read this in.csv in a system verilog function.
If i get “ANT” i need to store the value
e.g if I get ANT first time then its value will be 4 as in the input file
next time its value will be 6 as shown in the input file

I tried

function void read_ant();
int fd;
string c, str, ant;
int num;

fd= $fopen("in.csv", "r");
if(fd != 0) begin
 while($feof(fd)) begin
  c= $fgetc(fd);
  $ungetc(c, fd);
  $fgets(str,fd); 
          if(uvm_is_match( "ANT",str)) begin
            $fscanf(str, "%s %d", ant, num);
         end
  end
end

I don’t get cnt > 0

In reply to Anjali:

Not sure why you used $getc/$ungetc, snd you should be using $sscanf instead of $fscanf.

Also, it helps to put your question into a small complete example, like this:

module top;
   int fd;
   string c, str, ant;
   int num;
   initial begin
      if ((fd=$fopen("in.csv", "r")) != 0)
	while($fgets(str,fd) != 0) begin
	  if (str.match("ANT")) begin
	     // else: !if((fd=$fopen("in.csv", "r")) != 0)$sscanf(str, "%s %d", ant, num);
	     $display("%s :VALUE: %d", ant, num);
	  end

      end 
   end 
endmodule

In reply to dave_59:

Hi Dave,
Thanks for your reply. I tried the code, but got error for
str.match(“ANTSTARTF”))
This is not a valid built in method name for this object. [SystemVerilog].
if(str.match(‘ANTSTARTF’))begin

Just one more point here, the string to be matched is not “ANT”. It is “ANTSTARTF”
which is 9 characters. Not sure if $fgets can store it

Thanks,
Anjali

In reply to Anjali:

A number of tools have extended SystemVerilog to support the built-in method. You can continue to use uvm_is_match if yours doesn’t.

“ANTSTARTF” is new information. In any case, I’m not aware of any character limitation for $fgets. Some OS’s limit string sizes, but they are well above 1K.