Watching a File for Changes

  • Thread starter Christopher Harrison
  • Start date
C

Christopher Harrison

I would like to, cleanly, watch a flat file for changes (updates and
the like). The file will be altered by another program... I found some
example code (which actually didn't compile) on Google, but it
essentially does it the way I would have in the first place (i.e. sets
up a loop and gets the file's attributes each iteration, comparing to
the previous instance). This seems kind of cludgy to me: is there a
better way?

Thank you :)
Christopher Harrison
 
A

Alex Fraser

Christopher Harrison said:
I would like to, cleanly, watch a flat file for changes (updates and
the like). [...]

Then you should look into platform-specific methods, and if necessary ask in
the relevant group(s).

The closest you can get using standard C, the scope of this newsgroup, is to
make a copy of the file (either read it into memory or read it and write to
another file) and repeatedly compare the file with the copy. That certainly
isn't what I'd call "clean" :).

Alex
 
A

Antonio Contreras

Alex said:
Christopher Harrison said:
I would like to, cleanly, watch a flat file for changes (updates and
the like). [...]

Then you should look into platform-specific methods, and if necessary ask in
the relevant group(s).

The closest you can get using standard C, the scope of this newsgroup, is to
make a copy of the file (either read it into memory or read it and write to
another file) and repeatedly compare the file with the copy. That certainly
isn't what I'd call "clean" :).

You could also compute a CRC or a hash from the file, store it in
memory and compare in each iteration with the stored value. But this is
not much cleaner than simply copying the file and compare directly, and
eats much more processor time.
 
C

Christopher Harrison

I've been looking around and have found sys/stat.h which I'm currently
working with... I know I'm asking for trouble, but could someone
explain to me why this isn't "standard C". I read it's POSIX (although
it seems to be working OK under) Windows: what's the difference?
 
C

Chris Dollin

Christopher said:
I've been looking around and have found sys/stat.h which I'm currently
working with... I know I'm asking for trouble, but could someone
explain to me why this isn't "standard C".

It isn't defined by the C Standard (not any of them). By default,
the comp.lang.c interpretation of "the standard" is the appropriate
C standard.

Consequently, a C implementation is under no obligation to provide
a sys/stat.h, nor - if it does - for any functions inside to do
what you expect, /as far as the C standard is concerned/. Your
implementation may of course offer items taken from some /other/
standard ...
I read it's POSIX (although
it seems to be working OK under) Windows: what's the difference?

.... such as POSIX; the difference is that, generally speaking,
what POSIX offers isn't topical here.
 
C

Christopher Benson-Manica

Christopher Harrison said:
I've been looking around and have found sys/stat.h which I'm currently
working with... I know I'm asking for trouble, but could someone
explain to me why this isn't "standard C". I read it's POSIX (although
it seems to be working OK under) Windows: what's the difference?

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

Also...

It is proper Usenet etiquette to include the relevant portions of the text
you are replying to. To do this using Google groups, please follow the
instructions below, penned by Keith Thompson:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
M

Mark McIntyre

I've been looking around and have found sys/stat.h which I'm currently
working with... I know I'm asking for trouble, but could someone
explain to me why this isn't "standard C".

C supports hardware and OSes for which the functions in stat.h would
be either unimplementable or meaningless. Even for those for which a
posix layer exists, some of the functionality doesn't always make
sense, for example dev, ino, mtime, atime, nlink, gid, uid, in struct
_stat under DOS or Win9x.
I read it's POSIX (although
it seems to be working OK under) Windows: what's the difference?

POSIX is defined by one Standard, ISO C is defined by another.

FWIW posix provides a set of unix-like APIs which work on a range of
hardware that C cupports, but far from all of it.
 
W

William Ahern

Christopher Harrison said:
I would like to, cleanly, watch a flat file for changes (updates and
the like). The file will be altered by another program... I found some
example code (which actually didn't compile) on Google, but it
essentially does it the way I would have in the first place (i.e. sets
up a loop and gets the file's attributes each iteration, comparing to
the previous instance). This seems kind of cludgy to me: is there a
better way?

Judging from this thread you should take this to comp.unix.programmer.

There you can learn about things like dnotify, inotify, kqueue(2), and other
techniques (similar to sleep and stat). None of them can be done in what
plain C offers. They all require platform specific support (API's), and by
asking in the wrong group you're likely to get confusing or misleading
answers (either because the people are too experienced in a domain or not
enough, and the group isn't tuned for correcting and normalizing nontopical
subjects).
 
N

Netocrat


More directly:

POSIX (and the related term SUS) is an operating system standard for
UNIX-like operating systems. It extends the C standard but also applies
to more than just C. The searchable contents of the current standard
are here:
http://www.opengroup.org/onlinepubs/009695399/nfindex.html

This newsgroup does not deal with those extensions, it deals only with the
(quite limited) C standard itself. The current version of the standard is
C99; the more popularly implemented earlier version is C90 aka C89. You
can view free drafts of both (but must pay for a - only slightly variant -
final version) at:

http://dev.unicals.com/papers/c89-draft.html
http://dev.unicals.com/papers/c99-draft.html

If you are unsure whether something is POSIX-only or an extension of the C
standard, search those three sources.

Windows attempts to implement as much of POSIX as it can and apparently
stat.h is one of those areas.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top