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
How can we know whether a file is read or not
Started by swathin2, Sep 08 2009 03:21 AM
6 replies to this topic
#1
Posted 08 September 2009 - 03:21 AM
|
|
|
#2
Posted 08 September 2009 - 03:29 AM
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_*
Posted 08 September 2009 - 04:58 AM
Guest_h4x_*
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
Posted 08 September 2009 - 10:56 AM
#5
Posted 08 September 2009 - 12:41 PM
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.
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
Posted 08 September 2009 - 08:12 PM
#7
Posted 08 September 2009 - 08:37 PM
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


Sign In
Create Account


Back to top









