Archived Forum Post

Index of archived forum posts

Question:

Read last n lines using Chilkat FileAccess library

Jul 11 '17 at 10:51

I need to read only the last n lines from the file using Chilkat library (FileAccess) - https://www.chilkatsoft.com/refdoc/xCkFileAccessRef.html. Because the file size would be large(upto 5GB). I can able to read the last few records by using FileStream. But I want to do the same kind of parsing in Chilkat.

Parsing last few records in FileStream:

int sizeOfChar = 1;
Encoding _selectedEncoding = Encoding.ASCII;

using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
    long endPosition = 1024;

    for (long position = sizeOfChar; position < endPosition; position += sizeOfChar)
    {
        fs.Seek(-position, SeekOrigin.End);
    }

    byte[] extractedBuffer = new byte[fs.Length - fs.Position];
    fs.Read(extractedBuffer, 0, extractedBuffer.Length);
    string strTokens = _selectedEncoding.GetString(extractedBuffer);
}

This code is working fine as expected and returns last few lines. I have tried to achieve the same using Chilkat FileAccess class and it returns only empty array.

Parsing last few records in Chilkat.FileAccess:

int sizeOfChar = 1;

Chilkat.FileAccess fa = new Chilkat.FileAccess();
bool isFileOpened = fa.FileOpen(filePath, 0x80000000, 0x00000001, 4, 0x00000080);

int endPosition = 1024;

for (int position = sizeOfChar; position < endPosition; position += sizeOfChar)
{
    fa.FileSeek(-position, (int)SeekOrigin.End);
}

byte[] extractedBuffer = fa.FileRead(endPosition);
string strTokens = _selectedEncoding.GetString(extractedBuffer);

I can able to fetch first few lines using Chilkat but I want to get last few lines. Please check the above code block and anyone help me to parse the last few lines using Chilkat.


Answer

Thanks! The bug in LastNLines was discovered after the v9.5.0.68 release. I can create an ActiveX pre-release of .69 and post it here later today.


Answer

This new build should fix the problem:

32-bit: https://chilkatdownload.com/prerelease/chilkatax-9.5.0-win32-168.zip
64-bit: https://chilkatdownload.com/prerelease/chilkatax-9.5.0-x64-169.zip


Answer

For the FileSeek method, passing SeekOrigin.End is only valid if in fact SeekOrigin.End equals 2. Just pass the number 2. Chilkat is cross-platform/cross-language, so we can't use "SeekOrigin.End" because it doesn't apply across languages. We really need a specific number (i.e. 2 in this case).

Also, I don't quite understand the logic of using a loop. Just seek -1024 from the end if that's what you want..