![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2008
Posts: 10
Rep Power: 0
![]() |
wunix tail in windows
I am not sure what I need to do for this. I am trying to write a program that uses the tail command from Unix in windows. I am trying to get it to go to the bottom of the file and then print out however many lines that the user puts in from the bottom. I have got it where it opens a text file and I can get it to go to the bottom of the file using
fseek(fp, 0L, SEEK_END); |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
Re: wunix tail in windows
Yea, don't do that. What you're trying to do is awkward at best because moving backward by lines in a file is beyond the scope of a typical file setup. It doesn't mean you can't do it though, it just means that doing it involves jumping through hoops.
Try using a windowed approach. Read lines from the file sequentially and store the most recent N lines where N is the number of lines the user wants. This is a window of N lines. When the window is full and you read a new line, discard the oldest line and save the new line (hint: use a queue). With this approach you can easily handle files that have less than N lines, and if there are more than N lines, by the time you get to the end of the file the window will contain the last N lines.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 816
Rep Power: 4
![]() |
Re: wunix tail in windows
You don't necessarily want to go through the whole file, it could be gigabytes. I'd try jumping back a block at a time and counting the number of lines in each block.
When you have found enough lines, just start going forward again. You could probably save the lines that you find in a buffer, instead of reading them from the file again, but that would depend on how much memory you want to use. As to how big a block is, I'm not sure - I'd probably start with 4k but I couldn't tell you why, I just like 4k ![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Windows Installer Service Problems | Mjordan2nd | Coder's Corner Lounge | 2 | Jan 25th, 2008 2:01 PM |
| Windows XP Activation Loop... | Ghost | Coder's Corner Lounge | 3 | Jan 24th, 2008 3:28 PM |
| Linked list insertion at the tail | elford | Java | 9 | Jan 7th, 2006 9:04 AM |
| Windows Vista, well sort of | coldDeath | Coder's Corner Lounge | 21 | Sep 6th, 2005 1:00 PM |
| Net Group /ADD (on a windows box, non domain controller) | Infinite Recursion | Other Programming Languages | 1 | Apr 13th, 2005 2:27 PM |