I am working with the scanner of an interpreter that I am making, 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 the following, is there a way to not have to read the entire file and convert it into an array char
to use throughout the scan? What I am 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.
Your best bet is
StreamReader
the problem is the method you're using.ReadToEnd
read the entire file before continuing, instead you can try ReadLineThis processes the lines one at a time. Of course, this method will work only if you have a file separated by line changes (
"\n"
). If you need more control over how the file is read, the more advanced Read method is available.What you say is that you need to close and open the file at the last position, so you can do something like this:
You need to store the last position in the file but be aware that if you are writing to the file between reading and reading, the position may not be the same as before.