Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 30th, 2008, 8:48 AM   #1
starbeam
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0 starbeam is on a distinguished road
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);
but I dont know how to get it to go back to the start of that line and print it out and then keep going back until it gets to the number of lines that the user wanted printed. I know that a loop will have to be used. I was thinking a for loop but I just dont know how to get my program the go back to the start of that line once I get it to go to the bottom of the text file.
starbeam is offline   Reply With Quote
Old Jan 30th, 2008, 9:09 AM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
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.
Narue is offline   Reply With Quote
Old Jan 31st, 2008, 7:43 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
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
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:55 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC