1
\$\begingroup\$

I was trying to read a .csv file and use its contents. The csv is formatted as:

0001,1010,1110
0101,1100,1001

My code is:

`timescale 1 ns/10 ps  

module tb;
    
    reg a, b;
   
    reg[3:0] r1;
    reg[3:0] r2;
    reg[3:0] r3;

    integer i, f, j;

    initial
    begin 
        
        f = $fopen("vec.csv", "r");
        
        for (i = 0; i < 2; i = i +1)
        //while(! $feof(f))
        begin
        
            j = $fscanf(f,"%b %b %b",r1,r2,r3);
          
            #20;  
        end
        $display("%b,%b,%b", r1, r2, r3);

        $fclose(f);
        $display("pass");
    end
endmodule

The output is:

0001,xxxx,xxxx                                                                                                 
0001,xxxx,xxxx                                                                                                 
pass 

It seems that it can't read anything besides the first 4 bits of the 1st line. Could you tell me what I am doing wrong?

\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

Try inserting commas ,

      j = $fscanf(f,"%b,%b,%b",r1,r2,r3);
\$\endgroup\$
2
  • \$\begingroup\$ Thanks, what if there was 20 or more sets of 4 bits in every row? should I keep writing %b,%b,%b,.... for 20 times. is there a better way to do this? \$\endgroup\$ Aug 4, 2022 at 4:15
  • \$\begingroup\$ This is almost a completely new question. If you know exactly how many values are on each line, you can use $fscanf to read one value at a time in a loop. Otherwise you can read one line at a time using $fgets and then split the line into an array of values. This would be much easier to do in SystemVerilog using a package like SVlib. \$\endgroup\$
    – dave_59
    Aug 4, 2022 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.