Is it possible to read the contents of a file without keeping it in memory?

2

I'm working with the scanner of an interpreter that I'm doing, but I find that the class StreamReader becomes a bit slow with somewhat heavy files, so far I have the following:

using System.Collections.Generic;
using System.IO;
abstract class Scanner
{
    // Campos...
    private char[] Input;

    public virtual IList<Token> Scan()
    {
        // Código para escanear.
        return Tokens;
    }

    public Scanner(string _path, bool _debug)
    {
        // ...
        using (StreamReader sr = new StreamReader(_path))
            Input = sr.ReadToEnd().ToCharArray(); // Lee todo el archivo y lo convierte a un char[].
    }
}

My problem is this, is there a way to not have to read the entire file and convert it into an array of char to use it throughout the scan? What I'm looking for is to read the file, return the next character and close the file without keeping it in memory and remembering the position of the last character read.

    
asked by NaCl 17.05.2016 в 17:31
source

1 answer

1

Your best choice is StreamReader the problem is the method you are using. ReadToEnd reads the complete file before continuing, instead of this you can try with ReadLine

using (var reader = new StreamReader(path))
{
    string line;
    while ((line = reader.ReadLine()) != null) 
    {
        // analizar la línea actual
    }
}

This processes the lines one at a time. Of course, this method will only work if you have a file separated by line changes ( "\n" ). If you need more control over how the file is read, you have available the method Read which is more advanced.

What you are saying is that you need to close and open the file in the last position, then you can do something like this:

        using (var reader = new StreamReader("")) {
            reader.BaseStream.Seek(_lastPosition, SeekOrigin.Begin);

            string line;
            while ((line = reader.ReadLine()) != null) {
                if ( /* alguna condición para detener la lectura */) {
                    break;
                }
            }

            _lastPosition = reader.BaseStream.Position;
        }

You need to store the last position in the file but keep in mind that if you are writing to the file between reading and reading, the position may not be the same as before.

    
answered by 17.05.2016 / 19:05
source