I have problem in reading the file using $fscanf system function.
I am trying to read the read_pattern.txt file using $fscanf and
displaying the contents of the file.
read_pattern.txt is like this
101 100 25
110 111 32
The source code is
`define NULL 0
`define EOF 32'hFFFF_FFFF
module read_pattern;
integer c, r;
reg [2:0] bin1,bin2;
reg [8*10:1] str;
integer int,line;
integer file;
//reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */
initial
begin : file_block
$display("Bin1 Bin2 Int");
file = $fopen("read_pattern.txt","r");
if (file == `NULL) // If error opening file
disable file_block; // Just quit
r = 1;
while (r!==0)
begin
r = $fscanf(file," %b %b %d ", bin1,bin2,int);
$display ("Bin1 = %b Bin2 = %b Int = %d\n",bin1,bin2,int);
end // while not EOF
$system("cp read_pattern.txt output.log");
$fclose(file);
$finish;
end // initial
endmodule // read_pattern
When i invoke simulation command it is not stopping after displaying
the 2 lines.
The last line contents are continuosly displaying the with out exiting
the while loop.
Please explain why it is not exiting the while loop.
The simulation output is like this.
Bin1 = 101 Bin2 = 100 Int =25
Bin1 = 110 Bin2 = 111 Int =32
Bin1 = 110 Bin2 = 111 Int =32
Bin1 = 110 Bin2 = 111 Int =32
Bin1 = 110 Bin2 = 111 Int =32
Bin1 = 110 Bin2 = 111 Int =32
.......................
Regards
Vishnu
On Oct 8, 1:13 am, [email protected] wrote:
> I have problem in reading the file using $fscanf system function.
> I am trying to read the read_pattern.txt file using $fscanf and
> displaying the contents of the file.
>
> read_pattern.txt is like this
>
> 101 100 25
> 110 111 32
>
> The source code is
> `define NULL 0
> `define EOF 32'hFFFF_FFFF
> module read_pattern;
> integer c, r;
> reg [2:0] bin1,bin2;
> reg [8*10:1] str;
> integer int,line;
> integer file;
> //reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */
>
> initial
> begin : file_block
> $display("Bin1 Bin2 Int");
> file = $fopen("read_pattern.txt","r");
> if (file == `NULL) // If error opening file
> disable file_block; // Just quit
>
> r = 1;
> while (r!==0)
> begin
> r = $fscanf(file," %b %b %d ", bin1,bin2,int);
> $display ("Bin1 = %b Bin2 = %b Int = %d\n",bin1,bin2,int);
> end // while not EOF
> $system("cp read_pattern.txt output.log");
> $fclose(file);
> $finish;
> end // initial
> endmodule // read_pattern
>
> When i invoke simulation command it is not stopping after displaying
> the 2 lines.
> The last line contents are continuosly displaying the with out exiting
> the while loop.
> Please explain why it is not exiting the while loop.
>
> The simulation output is like this.
> Bin1 = 101 Bin2 = 100 Int =25
> Bin1 = 110 Bin2 = 111 Int =32
> Bin1 = 110 Bin2 = 111 Int =32
> Bin1 = 110 Bin2 = 111 Int =32
> Bin1 = 110 Bin2 = 111 Int =32
> Bin1 = 110 Bin2 = 111 Int =32
> ......................
> Regards
> Vishnu
try:
.. . .
r = 3;
while (r==3)
.. . .
It seems that EOF returns -1 rather than zero in "r".
Also note that even with the code above, you'll get
one extra repetition of your last line printed, since
the loop executes before "r" is checked.
On Oct 8, 9:41 am, gabor <[email protected]> wrote:
> On Oct 8, 1:13 am, [email protected] wrote:
>
>
>
> > I have problem in reading the file using $fscanf system function.
> > I am trying to read the read_pattern.txt file using $fscanf and
> > displaying the contents of the file.
>
> > read_pattern.txt is like this
>
> > 101 100 25
> > 110 111 32
>
> > The source code is
> > `define NULL 0
> > `define EOF 32'hFFFF_FFFF
> > module read_pattern;
> > integer c, r;
> > reg [2:0] bin1,bin2;
> > reg [8*10:1] str;
> > integer int,line;
> > integer file;
> > //reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */
>
> > initial
> > begin : file_block
> > $display("Bin1 Bin2 Int");
> > file = $fopen("read_pattern.txt","r");
> > if (file == `NULL) // If error opening file
> > disable file_block; // Just quit
>
> > r = 1;
> > while (r!==0)
> > begin
> > r = $fscanf(file," %b %b %d ", bin1,bin2,int);
> > $display ("Bin1 = %b Bin2 = %b Int = %d\n",bin1,bin2,int);
> > end // while not EOF
> > $system("cp read_pattern.txt output.log");
> > $fclose(file);
> > $finish;
> > end // initial
> > endmodule // read_pattern
>
> > When i invoke simulation command it is not stopping after displaying
> > the 2 lines.
> > The last line contents are continuosly displaying the with out exiting
> > the while loop.
> > Please explain why it is not exiting the while loop.
>
> > The simulation output is like this.
> > Bin1 = 101 Bin2 = 100 Int =25
> > Bin1 = 110 Bin2 = 111 Int =32
> > Bin1 = 110 Bin2 = 111 Int =32
> > Bin1 = 110 Bin2 = 111 Int =32
> > Bin1 = 110 Bin2 = 111 Int =32
> > Bin1 = 110 Bin2 = 111 Int =32
> > ......................
> > Regards
> > Vishnu
>
> try:
>
> . . .
>
> r = 3;
> while (r==3)
>
> . . .
>
> It seems that EOF returns -1 rather than zero in "r".
> Also note that even with the code above, you'll get
> one extra repetition of your last line printed, since
> the loop executes before "r" is checked.
>
> Cheers,
> Gabor
Another point... It seems that $fscanf is not line oriented
(I think this was also true in "C" as well). So even if you
add empty lines at the end of your file, the last scan will
eat all of the whitespace to the end of file and still return
-1 instead of 0.
On Oct 8, 6:49 pm, gabor <[email protected]> wrote:
> On Oct 8, 9:41 am, gabor <[email protected]> wrote:
>
>
>
> > On Oct 8, 1:13 am, [email protected] wrote:
>
> > > I have problem in reading the file using $fscanf system function.
> > > I am trying to read the read_pattern.txt file using $fscanf and
> > > displaying the contents of the file.
>
> > > read_pattern.txt is like this
>
> > > 101 100 25
> > > 110 111 32
>
> > > The source code is
> > > `define NULL 0
> > > `define EOF 32'hFFFF_FFFF
> > > module read_pattern;
> > > integer c, r;
> > > reg [2:0] bin1,bin2;
> > > reg [8*10:1] str;
> > > integer int,line;
> > > integer file;
> > > //reg [8*`MAX_LINE_LENGTH:0] line; /* Line of text read from file */
>
> > > initial
> > > begin : file_block
> > > $display("Bin1 Bin2 Int");
> > > file = $fopen("read_pattern.txt","r");
> > > if (file == `NULL) // If error opening file
> > > disable file_block; // Just quit
>
> > > r = 1;
> > > while (r!==0)
> > > begin
> > > r = $fscanf(file," %b %b %d ", bin1,bin2,int);
> > > $display ("Bin1 = %b Bin2 = %b Int = %d\n",bin1,bin2,int);
> > > end // while not EOF
> > > $system("cp read_pattern.txt output.log");
> > > $fclose(file);
> > > $finish;
> > > end // initial
> > > endmodule // read_pattern
>
> > > When i invoke simulation command it is not stopping after displaying
> > > the 2 lines.
> > > The last line contents are continuosly displaying the with out exiting
> > > the while loop.
> > > Please explain why it is not exiting the while loop.
>
> > > The simulation output is like this.
> > > Bin1 = 101 Bin2 = 100 Int =25
> > > Bin1 = 110 Bin2 = 111 Int =32
> > > Bin1 = 110 Bin2 = 111 Int =32
> > > Bin1 = 110 Bin2 = 111 Int =32
> > > Bin1 = 110 Bin2 = 111 Int =32
> > > Bin1 = 110 Bin2 = 111 Int =32
> > > ......................
> > > Regards
> > > Vishnu
>
> > try:
>
> > . . .
>
> > r = 3;
> > while (r==3)
>
> > . . .
>
> > It seems that EOF returns -1 rather than zero in "r".
> > Also note that even with the code above, you'll get
> > one extra repetition of your last line printed, since
> > the loop executes before "r" is checked.
>
> > Cheers,
> > Gabor
>
> Another point... It seems that $fscanf is not line oriented
> (I think this was also true in "C" as well). So even if you
> add empty lines at the end of your file, the last scan will
> eat all of the whitespace to the end of file and still return
> -1 instead of 0.