Jump to content

How can we know whether a file is read or not

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
swathin2

swathin2

    Newbie

  • Members
  • PipPip
  • 18 posts
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
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
The file itself does not contain any information regarding whether you've opened it or not (unless you save that information in the file, then of course it does), so if you want to track whether a file has been opened in a certain session of using your program or not, what I'd use is a function that detects all the files your program can open in a folder and maintains a map of some kind indicating which ones have been opened and which ones haven't. For example, you could create a function with a static char pointer, that is calloc'd to however many files are in the indicated folder at the time the program is opened, and then set each value to 0. While you're using the program, any time the file is opened, you'd call that function again with the name/number of the file that you're opening, and it would look up which char indicates that the file has been opened or not, and if it hasn't, it sets it to true (IE not zero) and returns 0 (indicating that the file hadn't been opened previously). It it had, it doesn't change anything, and returns 1 (indicating that the file had been opened previously). You can use this to check and set whether it was opened. :)
Wow I changed my sig!

#3
Guest_h4x_*

Guest_h4x_*
  • Guests
file is an object. os take name and do proper action (if its like aasd.txt) it check drive table and search for it, if it find, it return you handle (handle is a descriptor, i/o go through handle, os remember what it point to). If file not exist, or something else happen like insufficient permisions you get rax < 0 (sign bit set)

#4
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
A simple method is to keep a list of filenames that the user has opened somewhere.
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#5
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Isn't there some sort of information attached to the file that tells us when it was last accessed? Sort of like the last modified time in Windows?

If there is, you could simply keep a list of file access times, and whenever your program is run, you could check the current access time with the saved one to see if the file has been accessed.

#6
Aereshaa

Aereshaa

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 790 posts
Under unix, (linux & mac os x too), you can use the stat() function. stat
Watches: Nanoha, Haruhi, AzuDai. Listens to: E-Type, Dj Melodie, Nightcore.
"When people are wrong they need to be corrected. And then when they can't accept it, an argument ensues." - MeTh0Dz

#7
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts

Aereshaa said:

Under unix, (linux & mac os x too), you can use the stat() function. stat

For Example:
veda@veda-desktop:~$ stat eg.c 

  File: `eg.c'

  Size: 337       	Blocks: 8          IO Block: 4096   regular file

Device: 808h/2056d	Inode: 489479      Links: 1

Access: (0644/-rw-r--r--)  Uid: ( 1000/    veda)   Gid: ( 1000/    veda)

Access: 2009-09-08 16:16:22.000000000 +0530

Modify: 2009-09-08 16:16:28.000000000 +0530

Change: 2009-09-08 16:16:28.000000000 +0530