What is the most efficient way to read a large text file backwards?

What is the most efficient way to read a text file big back, line by line, using the Windows API functions ? For example, if the file:

line 1
...
line 108777
line 108778

the conclusion should be:

line 108778
line 108777
...
line 1

I want to write a C program for this. You don’t need to write code (but if you want, which is great), I’m just wondering how to do this, bearing in mind that the files are large and that I want the program to work as fast as it can.

Also, I'm interested in what features of the Windows API to use.

+3
source share
5 answers

- , ( - buffersize) () , u , , , .

+3

, , .

, .

+2

Files with memory mapping will fail (or at least become very complex) if the file is larger than the available address space. Instead, try the following:

input = input file
block_prefix = unique temporary file
block_index = 0

while (!eof (input))
{
   line = input.readline ();
   push line onto a stack

   if (stack > 100 entries) // doesn't have to be 100
   {
      output = block_prefix + block_index++

      while (stack has entries)
      {
        pop line off stack
        write to output
      }
   }
}

if (stack has entries)
{
  output = block_prefix + block_index++

  while (stack has entries)
  {
    pop line off stack
    write to output
  }
}

output = output file

while (block_index)
{
   read entire contents of block file (block_prefix + --block_index)
   write contents to output
   delete block file
}
+2
source

One way is to use the file offsets container at the beginning of each line. After parsing the file, process the container in the reverse order. See fgetc, fgetsand fseek.

+2
source

Source: https://habr.com/ru/post/1754794/


All Articles