Home > Archive > Unix Programming > September 2006 > getting file modified time of a file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
getting file modified time of a file
|
|
| Griffin486@gmail.com 2006-09-29, 8:01 am |
| Hello to all!!
i have been trying to read the modified time of a file throught a c
program.
its like a daemon.. in a big while(1) and keeps track of last modified
time..
The problem i m facing is.. When the program starts the modified time
it displays is correct
but if i modify the file e.g touch file_name..
my program still shows me old modified time :(
stat(conf.account_log_file, &acctFileAttrib); // get the
attributes of Acct file
prevModifiedTime = acctFileAttrib.st_mtime;
while(1)
{
//we entered here after two seconds so lets see if
acct file was modified
struct stat tmp;
memset(&tmp, 0, sizeof(struct stat));
//compare two files' modified time
stat("/root/testfile", &tmp); // get the attributes of Acct
file
//Now compare both attribs and see
char msg[256];
struct tm* t1 = NULL;
t1 = localtime(&tmp.st_mtime);
struct tm* t0 = NULL;
t0 = localtime(&prevModifiedTime);
sprintf(msg, "sec=%d, min=%d, hour=%d compared with
sec=%d, min=%d, hour=%d \n", t1->tm_sec, t1->tm_min, t1->tm_hour,
t0->tm_sec, t0->tm_min, t0->tm_hour);
log_message(LOG_INFO, msg);
prevModifiedTime = tmp.st_mtime;
}
any help :(
plzzzzzzzzz
| |
| Pascal Bourguignon 2006-09-29, 8:01 am |
| Griffin486@gmail.com writes:
> Hello to all!!
> i have been trying to read the modified time of a file throught a c
> program.
> its like a daemon.. in a big while(1) and keeps track of last modified
> time..
> The problem i m facing is.. When the program starts the modified time
> it displays is correct
> but if i modify the file e.g touch file_name..
> my program still shows me old modified time :(
>
> stat(conf.account_log_file, &acctFileAttrib); // get the
> attributes of Acct file
> prevModifiedTime = acctFileAttrib.st_mtime;
>
> while(1)
> {
> //we entered here after two seconds so lets see if
> acct file was modified
> struct stat tmp;
> memset(&tmp, 0, sizeof(struct stat));
> //compare two files' modified time
> stat("/root/testfile", &tmp); // get the attributes of Acct file
Are you developping and debugging a program under root account?
You really shouldn't be doing that!!!
> //Now compare both attribs and see
> char msg[256];
> struct tm* t1 = NULL;
> t1 = localtime(&tmp.st_mtime);
> struct tm* t0 = NULL;
> t0 = localtime(&prevModifiedTime);
You haven't read man localtime, have you?
> sprintf(msg, "sec=%d, min=%d, hour=%d compared with
> sec=%d, min=%d, hour=%d \n", t1->tm_sec, t1->tm_min, t1->tm_hour,
> t0->tm_sec, t0->tm_min, t0->tm_hour);
> log_message(LOG_INFO, msg);
>
> prevModifiedTime = tmp.st_mtime;
> }
--
__Pascal Bourguignon__ http://www.informatimago.com/
COMPONENT EQUIVALENCY NOTICE: The subatomic particles (electrons,
protons, etc.) comprising this product are exactly the same in every
measurable respect as those used in the products of other
manufacturers, and no claim to the contrary may legitimately be
expressed or implied.
| |
| Griffin486@gmail.com 2006-09-29, 7:01 pm |
|
Pascal Bourguignon wrote:
>
> Are you developping and debugging a program under root account?
> You really shouldn't be doing that!!!
yes i m developing and debugging under root accout..? does that matter?
>
>
> You haven't read man localtime, have you?
>
that was my mistak..but another question..
I run this program and run another simple program that only opens a
file in write mode and closes it..
will the modified time b updated in that case too? coz when i used
"touch file" it did show me updated time but in case of file opening
and closing it didn't.
any thoughts?
regards
| |
| Pascal Bourguignon 2006-09-29, 7:01 pm |
| Griffin486@gmail.com writes:
> Pascal Bourguignon wrote:
>
> yes i m developing and debugging under root accout..? does that matter?
This is very dangerous. The slightest error on your part, or the
slightest bug in the program might break your system.
It's like doing rope walking without a net.
> that was my mistak..but another question..
> I run this program and run another simple program that only opens a
> file in write mode and closes it..
> will the modified time b updated in that case too? coz when i used
> "touch file" it did show me updated time but in case of file opening
> and closing it didn't.
>
> any thoughts?
Well, opening and closing a file doesn't modify it. So I'd not be
surprized if the modification time wasn't updated. You'd have to read
again the POSIX standard to know what should be expected.
On more thing. Instead of doing active polling, you may happen to work
on a system that is able to send you a message when a file or a
directory is modified, like inotify. Then you should be using it
instead of polling.
http://www-128.ibm.com/developerwor.../l-inotify.html
--
__Pascal Bourguignon__ http://www.informatimago.com/
PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
| |
| jmcgill 2006-09-29, 7:01 pm |
| Pascal Bourguignon wrote:
> Are you developping and debugging a program under root account?
> You really shouldn't be doing that!!!
Hey, it's his system! ;-)
How else will he learn not to do it, unless he causes at least one disaster?
| |
| Griffin486@gmail.com 2006-09-30, 4:00 am |
|
jmcgill wrote:
> Pascal Bourguignon wrote:
>
>
> Hey, it's his system! ;-)
>
> How else will he learn not to do it, unless he causes at least one disaster?
very rite u r..
I was reli expecting a better reply as to WHAT is an alternative to NOT
DEVELOPING A PROGRAM UNDER ROOT.
This is a daemon kind of service that looks if some services are up and
running and if NOT running it starts those services.
btw I tried a simple program that opens a file and writes into it and
sleeps for 5 sec. and i was able to see the modified time by using the
'stat' command. but i don't get to see the update in my program..is
there anything i m missing..(The point is fclose only flushes the C
buffers according to man pages) so i used sync() too but no success :(
|
|
|
|
|