Home Forums C Programming How can we know whether a file is read or not Re: Re: How can we know whether a file is read or not

#3611
Chandra3993
Participant

Straight way you can not check the file is read or not.
For specific number of days or epoch seconds you can check

Here I have written the program for checking file is read in last 2 days


#include
#include
#include
#include
#include

main()
{
struct stat but ;
// Here you need to give the file name with absolute path.
stat("/home/pavunkumar/secrect",&but );
// Here I have calculated the one day epoch time.
int limit = 24*60*60;
// Getting the last read time of the file.
int read = but.st_atime ;
int current ;
// Getting the current epoch time.
current=time ( NULL ) ;
// Check file is not open for 2 days.
if (((current-read)/limit) >= 2 )
{
printf ( "file is not read ");
}
else
{
printf ( "file is read " ) ;
}
}