You are here:

C/Reading file contents when it's accessed

Advertisement


Question
-------------------------
Followup To
Question -
-------------------------
Followup To
Question -
Hi, The question subject might be a bit misleading. Anyway this is what I want to do. There is a particular log file that I want to access using C and read its contents and pass them to another process through a socket. I want to read the file as soon as it is accessed. I mean suppose some information is written to the log file. I want to know that and start reading those information immidiately. Still could't figure out a way to do that. Think you can help me?

Thanks
Nivantha.
Answer -
Is it the same process which is writing into the log file?

-ssnkumar

No. It's not the same process. It is another programm that's doing the logging. I want to monitor the log file and if it's being updated. Read the file and send those information over the network to another process.
Answer -
If it is another program, then that means another process!
Did you mean another function?

If it is just another function, then you can use signals.
If it is another program/process, then you need IPCs

-Narendra


It is a separate process. And it's a process that I have no control of. I think you are referring to some thing like a pipe by IPC. But it isn't possible to use a pipe when u don't have any control over the other process. So how to approach that? Can u please give me some more into?

Thanks
Nivantha.

Answer
If it is an outside process, which is not in your control, then there is only 1 way left with you.
Keep polling for change in the modification time of the log file. When modification time of a file changes, that indicates that the file has been accessed/edited.

You can use stat() function to query for the modification time of a file.

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.