Thread: How can we know whether a file is read or not

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    3

    How can we know whether a file is read or not

    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

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Check the time stamp, perhaps?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > how can we know whether a particular file is read or not using c programming.
    In general, you can't.

    Some file systems maintain a "last access", but that could mean anything (not just reading).
    Then there is the problem of if the filesystem is mounted read-only, in which case the last access time directory entries won't be updated.

    Then there is the whole "boot another OS" and read the file issue, not to mention "bypass the file system and read the raw disk" issue.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    On Windows, I saw some Windows API functions that you could subscribe to which notify you when a file is accessed. I forget their names, but you can look them up. I'm not sure about UNIX though.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cpjust View Post
    I'm not sure about UNIX though.
    Hmmm. This came up tangentially today. There is a Glib function, g_io_add_watch(), which can be used with a "socket, pipe, or file". I've used it with a socket, but not a file; I would assume something has to be added to the file to trigger, but you never know.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM