0

I have textfiles used for printers that are between 4M and 1G: it has very important text right at the end of these files for "no of pages", that we need to extract.

I've got code for Reading a block one big chunk at a time, below

Private Function GetBlock(reader As StreamReader) As String
        Dim builder As New System.Text.StringBuilder
        Dim buffer(m_BlockSize - 1) As Char
        Dim charCount As Integer

        'Read the next 4KB of the file.
        charCount = reader.ReadBlock(buffer, 0, m_BlockSize)

        builder.Clear()
        builder.Append(buffer, 0, charCount)

        Return builder.ToString()

End Function

I've seen code on the internet to read the blocks from the bottom up, but can't seem to locate it now.

Does anyone know how to read from the bottom up, to save a massive amount of time traversing through files upto 1G of text in size?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Not terribly clear what you want here. You can position the FilePointer to just before the end but then it becomes interesting whether you have ASCII or UTF8 inside. – H H Feb 16 '17 at 15:24
  • Is it possible to save the whole text in a string? Or will it give exception? – Aditi Feb 16 '17 at 15:25
  • 1
    see: [http://stackoverflow.com/a/452945/2592875](http://stackoverflow.com/a/452945/2592875). – TnTinMn Feb 16 '17 at 15:32
  • Aditi - Apologies, Yes it gives an OutOfMemory exception at the higher end of FileSizes. That is why we've opted to use ReadBlock : read, write to output file, and then dump about 4K in memory, and move onto the next 4K. It's really good, but overkill for getting this single item "No of Pages" right at the end of the file. Think it reads into an buffer array and then reverse it, possibly! – Stuart Kearney Feb 16 '17 at 15:39
  • 2
    You can pass a FileStream to a StreamWriter. Use FileStream.Seek() first to move it near the end. First one or two ReadLine() calls probably produce junk but the next ones should sync up. – Hans Passant Feb 16 '17 at 16:14
  • Arhh seek, let me have a look at that. Possibly the needle in the haystack. Thk you – Stuart Kearney Feb 17 '17 at 09:50

0 Answers0