|
Location: Desktop development - C/C++ License: The Intelliproject Open License (IPOL) Windows porting of tailf command from LinuxPosted by Silviu CarageaThis is a port of tailf on Windows. tailf is a program on Unix and Unix-like systems used to display the last few lines of a text file or piped data. |
Skill: BeginnerPosted: 17/12/2008Views: 624Rating: 0.00 /5Popularity: 0.00 |
| Sign Up to vote for this article |
The UNIX command tail -f or tailf are indispensible for monitoring log files in real-time. tail has a special command line option -f (follow) that allows a file to be monitored. Instead of displaying the last few lines and exiting, tail displays the lines and then monitors the file. As new lines are added to the file by another process, tail updates the display.
This is particularly useful for monitoring log files. The following commands will display the last 10 lines of messages and append new lines to the display as new lines are added to messages:
tail -f /var/adm/messages
or
tailf -n 10 /var/adm/messages
tailf is similar to tail -f, but it does nothing when the file is not growing.To interrupt tail or tailf while it is monitoring, break-in with Ctrl+C.
Because this very useful command is not available in windows world I decided to port the Linux code on windows. It's not a bid deal. I just replaced some headers, I had changed some data types and functions and works!

First of all we have static int tailf(const char *filename, int lines). This function will display the last n lines from a file.
The following function it's used to appends new lines to the display when new lines are added to the file.The algorithm is pretty simple: It checks if the file size is changed and display only the appended lines of data.
The following function it's used to loop and to continuously check if any lines are appended to the file.
That's all. Of course the application it have a main function where it parse the command line to see which file is monitoring and how much last lines will display .
This article, along with any associated source code and files, is licensed under The Intelliproject Open License (IPOL)
| Silviu Caragea
| Silviu Caragea is the Founder, Administrator and Chief Editor who wrote and runs The IntelliProject. He's been programming since 2000 and now he's student at The Faculty of Economic Cybernetics, Statistics and Informatics from Bucharest. In the same time he's working as software developer at Cratima Software, a Romanian software and web design company that activates both on the local and foreign market, providing its customers with software development services, internet and intranet solutions, web design, graphic design and IT consultancy. His programming experience includes: - C,C++, Visual C++(Win32 API, MFC, ADO, STL, DAO, ODBC, ATL, COM, DirectShow, DirectDraw, WTL) - Open Source libraries :CURL & Boost - HTML, CSS - Java (SE,ME) - JavaScript, Ajax, Google Web Toolkit (GWT) - Php, MySQL -Oracle, PL SQL - C# .NET -Objective C, IPhone SDK, Cocoa Location: |
Sign up to post message on the article message board!