Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a file backwards?

Tags:

c++

Is there a way to read a file backwards, line by line, without having to go through the file from the beginning to start reading backwards?

like image 944
mezamorphic Avatar asked May 30 '12 09:05

mezamorphic


People also ask

How do you read a file in reverse order?

An efficient solution to read a file in reverse order is, Start reading the file from last and continue till the start of the file i.e. in reverse order. As soon as it encounter any '\n' then it means, a complete line is read.

What Linux command can read a file content in reverse?

Tac. Another exciting way to display the contents of a file in Linux is in reverse order. To do so, use the tac command. It is similar to cat but reversed, reading and displaying the file starting from the last line.

How do you search backwards less?

To open a file using less, type the less command followed by the file name. You can search for text with vi commands such as / (look forward) and ? (look backward). You can page down with f (forward) and up with b (backward).


1 Answers

Use a memory-mapped file and walk backwards. The OS will page in the needed parts of the file in reverse order.

like image 90
Roger Lipscombe Avatar answered Oct 19 '22 08:10

Roger Lipscombe