FPGA Central - World's 1st FPGA / CPLD Portal

FPGA Central

World's 1st FPGA Portal

 

Go Back   FPGA Groups > NewsGroup > Verilog

Verilog comp.lang.verilog newsgroup / usenet

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-08-2007, 06:13 AM
Guest
 
Posts: n/a
Default Reading file using $fscanf

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

Reply With Quote
  #2 (permalink)  
Old 10-08-2007, 02:41 PM
gabor
Guest
 
Posts: n/a
Default Re: Reading file using $fscanf

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

Reply With Quote
  #3 (permalink)  
Old 10-08-2007, 02:49 PM
gabor
Guest
 
Posts: n/a
Default Re: Reading file using $fscanf

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.

Reply With Quote
  #4 (permalink)  
Old 10-10-2007, 05:48 AM
Guest
 
Posts: n/a
Default Re: Reading file using $fscanf

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.


Thankyou i got it

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading multiple columns from a single file kaush Verilog 6 08-23-2006 08:20 AM
TetraMax Model:: Basic file reading serially (like ROM) AArora Verilog 0 10-19-2005 03:36 PM
READING FROM MEMORY FILE naveend Verilog 2 12-06-2004 05:42 AM
Reading a pgm image file from testbench apai Verilog 0 10-15-2004 01:52 PM
reading file using $fscanf ndesi Verilog 4 05-21-2004 08:32 PM


All times are GMT +1. The time now is 11:40 AM.


Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Copyright 2008 @ FPGA Central. All rights reserved