[algogeeks] Re: how to implement TAIL command of unix.

2010-08-22 Thread R.ARAVINDH
read the i/p file
count the no. of '\n' characters
if count  k (argument of tail) then print all chars till EOF

correct me if m wrong !!!

On Aug 16, 9:16 am, vikas kumar vikas.kumar...@gmail.com wrote:
 the method of farword seek is inefficient. consider case of 10
 lines and you want to display only 3-4 lines. better seek from end.

 se a buffer[buf_size].

 let size =filesize.
 lc = 0;
 while(lc = given line input)
 {
 fseek(fp, size);
 if(size  buf_size)
   fread(fp, size, buffer);
 else
   fread(fp, size, buf_size);

 parse buf_size for '\n'
  if( \n is in buffer )
    increment line counter(lc ++)
 if(size  buf_size)
    size -= buf_size

 }

 // you know the size, read the buffer one by one and print it
 OR
 // you could have put them while reading on to stack and print it out
 now

 On Aug 15, 8:46 am, Dave dave_and_da...@juno.com wrote:



  Enter the lines into a FIFO queue as you read them. After you have
  enqueued n lines, dequeue a line every time you enqueue one, so that
  the queue will contain the last n (or fewer) lines of the file.

  Dave

  On Aug 13, 1:13 pm, amit amitjaspal...@gmail.com wrote:

   I am trying using fseek but somehow its not working?- Hide quoted text -

  - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: how to implement TAIL command of unix.

2010-08-16 Thread vikas kumar
the method of farword seek is inefficient. consider case of 10
lines and you want to display only 3-4 lines. better seek from end.

use a buffer[buf_size].

let size =filesize.
lc = 0;
while(lc = given line input)
{
fseek(fp, size);
if(size  buf_size)
  fread(fp, size, buffer);
else
  fread(fp, size, buf_size);

parse buf_size for '\n'
 if( \n is in buffer )
   increment line counter(lc ++)
if(size  buf_size)
   size -= buf_size
}

// you know the size, read the buffer one by one and print it
OR
// you could have put them while reading on to stack and print it out
now


On Aug 15, 8:46 am, Dave dave_and_da...@juno.com wrote:
 Enter the lines into a FIFO queue as you read them. After you have
 enqueued n lines, dequeue a line every time you enqueue one, so that
 the queue will contain the last n (or fewer) lines of the file.

 Dave

 On Aug 13, 1:13 pm, amit amitjaspal...@gmail.com wrote:



  I am trying using fseek but somehow its not working?- Hide quoted text -

 - Show quoted text -

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: how to implement TAIL command of unix.

2010-08-14 Thread Prem Mallappa
Tail by default displays last 10 lines of file.

1. mmap the file
2. keep two pointers(A, B) pointing to beginning of the file
2. search for 10th \n using B, if not found i.e file has less than
10 lines, print from beginning to end
3. if found, start incrementing both A and B to the next \n. untill
B reaches end of file.
4. Print from A till end of file.



On 13 Aug, 23:13, amit amitjaspal...@gmail.com wrote:
 I am trying using fseek but somehow its not working?

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: how to implement TAIL command of unix.

2010-08-14 Thread Gene
Tail works on stdin, too.  Can't mmap that.  The usual way is to
buffer the last N lines read in a ring buffe.r

On Aug 14, 4:22 pm, Prem Mallappa prem.malla...@gmail.com wrote:
 Tail by default displays last 10 lines of file.

 1. mmap the file
 2. keep two pointers(A, B) pointing to beginning of the file
 2. search for 10th \n using B, if not found i.e file has less than
 10 lines, print from beginning to end
 3. if found, start incrementing both A and B to the next \n. untill
 B reaches end of file.
 4. Print from A till end of file.

 On 13 Aug, 23:13, amit amitjaspal...@gmail.com wrote:



  I am trying using fseek but somehow its not working?

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: how to implement TAIL command of unix.

2010-08-14 Thread Dave
Enter the lines into a FIFO queue as you read them. After you have
enqueued n lines, dequeue a line every time you enqueue one, so that
the queue will contain the last n (or fewer) lines of the file.

Dave

On Aug 13, 1:13 pm, amit amitjaspal...@gmail.com wrote:
 I am trying using fseek but somehow its not working?

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algoge...@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.