0

I'm trying to Seek to a Position in a File and want to go with ReadLine after that. FPosition is stored before and matches the beginning of a specific line in the file. Maybe useful: my file has a size of 20gB and is a textfile.

private List<int> FPosition = new List<int>();

(...)

public List<string> LoadFile(string aFile, int aPage, int aRowsPerPage)
{
    using (var fileStream = File.OpenRead(aFile))
    {
        if (FPosition.Count <= (aPage - 1) * aRowsPerPage)
        {
            fileStream.Seek(FPosition[(aPage - 1) * aRowsPerPage], SeekOrigin.Begin);
        }
        using (var streamReader = new StreamReader(fileStream,
                                                 Encoding.Unicode,
                                                 true,
                                                 BufferSize))
        {
            for (int i = 0; aRowsPerPage - 1; i++))
            {
                myStringList[i] = streamReader.ReadLine;
            }
        }
    }
    return myStringList;
}

At which position do I have to do that seek?

Thanks for every help

Greaka
  • 735
  • 7
  • 16
  • 1
    What's the expected result exactly? – Philippe Paré Sep 28 '15 at 13:41
  • Can you show more code? – Cjen1 Sep 28 '15 at 13:54
  • i hope that clears a bit – Greaka Sep 28 '15 at 14:34
  • The same problem/issue with `StreamReader` is answered here: [StreamReader and seeking](http://stackoverflow.com/questions/5404267/streamreader-and-seeking) (duplicate) – MicroVirus Sep 28 '15 at 14:52
  • theres a wonderful "update" in the comment, right like it says, its not the actual position and i cannot use the _bytesRead – Greaka Sep 28 '15 at 14:54
  • @Greaka The way I understood it is that it works for setting, but you can't 'get' the current position of the stream after reading, because the `StreamReader` does its own buffering. So you can set the position before you perform the first read and then read from that point onwards. Anything else seems to be unspecified on if it'll actually work. – MicroVirus Sep 28 '15 at 17:05
  • @MicroVirus I try it with some code I found on another website, maybe I need help again when code is ready for debug, ty so far (: – Greaka Sep 29 '15 at 07:49

0 Answers0