linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* how to read a constantly changing file
@ 2002-06-04  9:36 Mohammed Khalid Ansari
  2002-06-04  9:54 ` Steven Smith
  0 siblings, 1 reply; 4+ messages in thread
From: Mohammed Khalid Ansari @ 2002-06-04  9:36 UTC (permalink / raw)
  To: linux c programming mailing list


Hi,

I want to know how can I read a file (through a C code) which is 
constatnly changing, like a log file.(eg tail command)

Can anyone write me a sample code.

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to read a constantly changing file
  2002-06-04  9:36 how to read a constantly changing file Mohammed Khalid Ansari
@ 2002-06-04  9:54 ` Steven Smith
  2002-06-05 11:39   ` Mohammed Khalid Ansari
  0 siblings, 1 reply; 4+ messages in thread
From: Steven Smith @ 2002-06-04  9:54 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list

[-- Attachment #1: Type: text/plain, Size: 478 bytes --]

> I want to know how can I read a file (through a C code) which is 
> constatnly changing, like a log file.(eg tail command)
As far as I can make out, tail does it by sitting in a loop
and calling sleep(1), and then fstat() to see if the file's grown.

You might be better off looking at directory change notifications
for the parent directory. The closest thing this has to documentation
is fs/dnotify.c in the kernel source, though.

Steven Smith,
sos22@cam.ac.uk.

[-- Attachment #2: Type: application/pgp-signature, Size: 232 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to read a constantly changing file
  2002-06-04  9:54 ` Steven Smith
@ 2002-06-05 11:39   ` Mohammed Khalid Ansari
  2002-06-05 16:12     ` Glynn Clements
  0 siblings, 1 reply; 4+ messages in thread
From: Mohammed Khalid Ansari @ 2002-06-05 11:39 UTC (permalink / raw)
  To: Steven Smith; +Cc: linux c programming mailing list



That's ok but it is not feasible to open a file every time it changes, go 
to the last bytes which appended to the file and print it (tail command) 
especially when the filesize is bigger (eg log file). There must be some 
other method.

-- 

**************************************************************************

Mohammed Khalid Ansari                    Tel (res) : 0091-022-3051360
Assistant Manager II                          (off) : 0091-022-2024641
National Centre for Software Technology   Fax       : 0091-022-2049573 
8th flr,Air India Build. Nariman Point,   E-Mail    : khalid@ncst.ernet.in 	
Mumbai 400021.

Homepage : http://soochak.ncst.ernet.in/~khalid			  	  

**************************************************************************

On Tue, 4 Jun 2002, Steven Smith wrote:

> > I want to know how can I read a file (through a C code) which is 
> > constatnly changing, like a log file.(eg tail command)
> As far as I can make out, tail does it by sitting in a loop
> and calling sleep(1), and then fstat() to see if the file's grown.
> 
> You might be better off looking at directory change notifications
> for the parent directory. The closest thing this has to documentation
> is fs/dnotify.c in the kernel source, though.
> 
> Steven Smith,
> sos22@cam.ac.uk.
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: how to read a constantly changing file
  2002-06-05 11:39   ` Mohammed Khalid Ansari
@ 2002-06-05 16:12     ` Glynn Clements
  0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2002-06-05 16:12 UTC (permalink / raw)
  To: Mohammed Khalid Ansari; +Cc: linux c programming mailing list


Mohammed Khalid Ansari wrote:

> > > I want to know how can I read a file (through a C code) which is 
> > > constatnly changing, like a log file.(eg tail command)
> > 
> > As far as I can make out, tail does it by sitting in a loop
> > and calling sleep(1), and then fstat() to see if the file's grown.
> 
> That's ok but it is not feasible to open a file every time it changes, go 
> to the last bytes which appended to the file and print it (tail command) 
> especially when the filesize is bigger (eg log file). There must be some 
> other method.

Who said anything about repeatedly opening it? Just keep reading the
file; if you hit EOF, sleep for a while, then continue. E.g.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
	int fd;

	if (argc != 2)
	{
		fprintf(stderr, "usage: %s <filename>\n", argv[0]);
		return 1;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0)
	{
		perror("open");
		return 1;
	}

	if (lseek(fd, 0, SEEK_END) < 0)
	{
		perror("lseek");
		return 1;
	}

	for (;;)
	{
		char buff[0x10000];
		int n = read(fd, buff, sizeof(buff));
		if (n < 0)
		{
			perror("read");
			break;
		}
		if (n == 0)
		{
			sleep(1);
			continue;
		}
		if (write(STDOUT_FILENO, buff, n) < 0)
		{
			perror("write");
			break;
		}
	}

	return 1;
}

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-06-05 16:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-06-04  9:36 how to read a constantly changing file Mohammed Khalid Ansari
2002-06-04  9:54 ` Steven Smith
2002-06-05 11:39   ` Mohammed Khalid Ansari
2002-06-05 16:12     ` Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).