RegisterLogin  
Update Profile
   
You are here: Forum  
Minimize 
SVUG Community Forum
Subject: Issue with $feof()
Prev Next
You are not authorized to post a reply.

Author Messages
Nithin KumarUser is Offline

Posts:4

12/21/2009 3:54 AM  
 Hi all,

I am new to SystemVerilog.

I have one doubt regarding files.Can you look into this  .!!!.
I want to read from one file and display the contents of the file.I wrote the following code


module file_rd_dis();

string strΕ] ;
int i,j,r,file;

initial begin
string s;
file =$fopen("switch.txt","r");
while (!$feof(file))begin
   r= $fscanf(file,"%s",s);
   str =s;
   i++;
end
$fclose(file);
foreach(str[j])
   $display("index j= %d  string =%s",j,str[j]);
#4 $finish;
end
endmodule




switch.txt

sachin
dravid
ganguly
kumble
sehwag
zaheer
~
~





Output is coming like this
index j=           0  string =sachin
index j=           1  string =dravid
index j=           2  string =ganguly
index j=           3  string =kumble
index j=           4  string =sehwag
index j=          5  string =zaheer
index j=          6  string =zaheer
  //Displaying two times.

switch.txt contains only six strings but while loop is iterating seven times.Its getting "feof" character after seventh iteration even though i gave only six strings.Can you look into this..?

if i give string array size 6 instead of 7 its showing  below error
 Error-[OBA] Out of bound access
file_rd_dis.sv, 11
  Out of bound access on array (index value: 7)
  Please compile with "-ntb_opts check" option and run to get more
  information.


Thanks,
Nithin

SHALOMUser is Offline

Posts:43

12/21/2009 4:13 AM  

The standard says that $feof "returns a nonzero value when EOF has previously been detected reading the input file fd. It returns zero otherwise."

The key word is "previously".

Shalom

Nithin KumarUser is Offline

Posts:4

12/21/2009 8:58 AM  
Hi Shalom,

The standard says that $feof "returns a nonzero value when EOF has previously been detected reading the input file fd. It returns zero otherwise."




You meant to say,$feof returns nonzero value when it reads EOF character as normal character.
Can you eleborate that point. I didnt get that what does previous mean..?


Thanks,
Nithin

SHALOMUser is Offline

Posts:43

12/21/2009 9:09 AM  
I mean that the loop is executed 7 times, because EOF is only detected on the 7th attempt to read the file.

Shalom
Nithin KumarUser is Offline

Posts:4

12/23/2009 3:25 AM  


We should never use feof() as the exit indicator for a loop. feof() is TRUE only after the end of file (EOF) is read, not when EOF is reached.

In the given example only five strings are there and then EOF present.So loop will iterate five times ($feof returns zero) and sixth time it will read EOF character.For EOF character also $feof returns zero but $fscanf cant read this character,the string variable hold the previous value and displays the same.

We can avoid the Issue by writing like this

"while ($fgets(s,file)!= null)"


Thanks,
Nithin
NishitUser is Offline

Posts:1

02/11/2010 3:00 PM  

Does the $fgets move the pointer in the file? So basically if I do $fgets to check for eof and if not then when I do a $fscanf then will I be reading the same line or would I be reading the next?

Thanks,

-Nishit
You are not authorized to post a reply.



ActiveForums 3.7
  

 Copyright 2008 by SystemVerilog User Group Contact Us    Privacy Statement