Click here to Skip to main content
14,604,663 members

C++ Win32 Command Line Tail - Made Easy

Rate this:
1.95 (15 votes)
Please Sign up or sign in to vote.
1.95 (15 votes)
2 Apr 2006
Tail Program - Similar to unix tail - I found tail programs - but not a good CUI project. It runs continuously and prints the last characters as soon as the file gets updated. Coding is very easy - and one can reuse and move further with it

Sample Image - Tail.jpg

Introduction

This is a command line tail program which prints the last updated characters of a file. One can use this to view the log files.

This program keeps on running and as soon as the file gets updated, the last characters are shown.

Usage

Tail [Absolute File Path]

Example:

tail c:\demo.txt
Above command shows the last characters of the file as soon as it is updated and waits again for the file to be updated

About The Code

Code is very simple - still some notes on it.

  • getftime - Small function to get the File Timestamp in character array.
  • getfnoc - Returns number of characters of a file
  • Logic: First I opened the file, got its timestamp and total characters present. Next in a loop - with sleep delay, i am checking the timestamp with initial one, and if there is diffrence, just printing the added characters and updating the variable for total characters. Thats All. Very Easy.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

kbfromindia
Web Developer
India India
Hi, I am a simple programmer and try to make things as simple as possible.

I code in c++, vb, perl etc. and try making simple objects out of things which look very hard

Comments and Discussions

 
GeneralMy vote of 2 Pin
Member 37857014-Mar-09 4:21
MemberMember 37857014-Mar-09 4:21 
GeneralDon't work Pin
robertomir19-May-07 2:11
Memberrobertomir19-May-07 2:11 
GeneralSome issues Pin
prcarp3-Apr-06 4:19
Memberprcarp3-Apr-06 4:19 
1) Error checking.
If the argv[1] file does not exist, _stat returns -1. The following strcpy() crashes because ctime returns a NULL. I would also put error checking in other parts of the code. For instance, what if the file is locked for reading (no share).

2) Multiple appended lines.
If the file being examined adds several lines while your tail program sleeps, only the last line is displayed. I would put fnol outside your loop so that it is remembered; then you can test against it to display _all_ the lines that are added. This is more like the Unix tail utility.

3) Wheel re-invented.
This utility is presented in many places on the web, including here on codeproject (albeit window-ized). Some are better, some are worse. Just google tail.c and you will see what I mean.

I don't mean to be too harsh here. You just may want to put a little more effort into the code & article before presenting it. It still is a useful utility.
GeneralRe: Some issues Pin
kbfromindia3-Apr-06 20:39
Memberkbfromindia3-Apr-06 20:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 2 Apr 2006

Stats

37.9K views
814 downloads
12 bookmarked