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

Viewing 1 reply thread
  • Author
    Posts
    • #2230
      Archie7891
      Participant

      how can we know whether a particular file is read or not using c programming.

      for an example:

      the file name = abcd.txt

      the status of the file should be 0 if the person has not opened it and it should be changed to 1 after any one reads it

      how can we do this

    • #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 " ) ;
      }
      }

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.