How to find the EOF while reading usint $fscanf

Guest
Hi
I am trying to read one file using $fscanf. But eventhough End of file
is reached, if i tried to read it will displaly the last data which it
got while fscanf. How i will know EOF is reached.
For example
x.log has follwing data
987cba
187cba
927cba
983cba
9874ba

If i tried to read above code using following block.

initial
begin
r4 = $fopen("x.log","r");
for (i = 0 ; i < 10; i = i + 1)
begin
t2 = $fscanf(r4,"%h",vr);
$display("%h",vr);
end
i will get follwing output
# 987cba
# 187cba
# 927cba
# 983cba
# 9874ba
# 9874ba
# 9874ba
# 9874ba
# 9874ba
# 9874ba

Eventhogh EOF is reached, it will display the last scanf. How i will
know i have reached while using $fscanf
 
On Wed, 30 Jan 2008 00:32:03 -0800 (PST), vishnuprasanth@gmail.com
wrote:

Hi
I am trying to read one file using $fscanf. But eventhough End of file
is reached, if i tried to read it will displaly the last data which it
got while fscanf. How i will know EOF is reached.
You need to check the result of fscanf for EOF (end of file) which is
-1 as defined in the spec so:
t2 = $fscanf(r4,"%h",vr);
if (t2 == -1)
$finish;
else ...

should work.
 
It's not listed in the LRM, but I think all the major simulators
support $feof. Call it with your filehandle and it returns non-zero
for EOF and 0 for non-EOF.

for (i = 0 ; i < 10; i = i + 1)
begin
if ($feof(r4))
$finish; // or break or disable or whatever
else
begin
t2 = $fscanf(r4,"%h",vr);
$display("%h",vr);
end
end

If it's not supported by your simulator directly but SystemVerilog is,
you can call the C library version by creating a DPI import
declaration for it. That's one of the hidden niceties of DPI--you can
call any standard C library function.

-cb
 
On Jan 30, 11:06 am, Chris Briggs <ch...@engim.com> wrote:
It's not listed in the LRM, but I think all the major simulators
support $feof.
It was added in the 2005 LRM.


If it's not supported by your simulator directly but SystemVerilog is,
you can call the C library version by creating a DPI import
declaration for it. That's one of the hidden niceties of DPI--you can
call any standard C library function.
To do that, you would have to use the C library for all your file I/
O. There is no standard mechanism to translate between Verilog and C
file descriptors. You cannot just pass a Verilog file descriptor to a
C library function and expect it to work.
 
On Feb 1, 1:00 pm, sh...@cadence.com wrote:
On Jan 30, 11:06 am, Chris Briggs <ch...@engim.com> wrote:
If it's not supported by your simulator directly but SystemVerilog is,
you can call the C library version by creating a DPI import
declaration for it. That's one of the hidden niceties of DPI--you can
call any standard C library function.

To do that, you would have to use the C library for all your file I/
O. There is no standard mechanism to translate between Verilog and C
file descriptors. You cannot just pass a Verilog file descriptor to a
C library function and expect it to work.
Steve,

Thanks. That's good to know. I've used the feature a couple of times,
though not for file I/O, and now I know not to. Fortunately, I don't
anticipate needing to as they've covered the standard file I/O
functions thoroughly enough, I think.

-cb
 

Welcome to EDABoard.com

Sponsor

Back
Top