Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   wunix tail in windows (http://www.programmingforums.org/showthread.php?t=15086)

starbeam Jan 30th, 2008 9:48 AM

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.

Narue Jan 30th, 2008 10:09 AM

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.

The Dark Jan 31st, 2008 8:43 PM

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 :)


All times are GMT -5. The time now is 3:53 AM.

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