I am re-reading the /proc/stat
file to get the updated values. First I opened the file, read each line, closed the file and re-opened to get the updated values. I want to know if closing a file is necessary or I can achieve the same effect by seeking to starting of the file. I wrote a code which did not close the file, instead placed the file pointer to the beginning of the file, which worked as before. But I wanted to know that will seeking to the starting of the file gurantee that I will get the updated information?
Edit: Another point, I am sleeping between the seeks and reads.
Although this question is not language specific here are the implementation languages in context: C, Perl.
EDIT
Here is the code I wrote.
while ()
{
open (STAT, "/proc/stat") or die "Cannot open /proc/stat\n";
while (<STAT>)
{
#Stuff here
}
close (STAT);
sleep 1;
}
Vs.
open (STAT, "/proc/stat") or die "Cannot open /proc/stat\n";
while ()
{
seek STAT, SEEK_SET, 0;
while (<STAT>)
{
#Stuff here
}
sleep 1;
}
close (STAT);
Which one is preferable?