Is it possible to get a specific line of txt / csv file using an address?

3

Is it possible to access a line from a file txt / csv (example below) at its address directly without having to go through each line using C #?

    
asked by anonymous 17.10.2014 / 20:06

1 answer

6

You can save this data that you took on the line along with the position of the file where that line starts to be able to access that portion of the file directly at a later time.

Read a specific position of the file in C # use the FileStream Seek method :

using (FileStream fs = new FileStream(@"file.txt", FileMode.Open, FileAccess.Read)) {
    fs.Seek(100, SeekOrigin.Begin); //100 é a posição onde inicia a linha desejada
    byte[] buffer = new byte[200]; //nada garante que a linha não seja maior que 200
    fs.Read(buffer, 0, 200));
}

I placed GitHub for future reference.

This guarantees random access to any part of the file directly. The SeekOrigin.Begin indicates that the position is relative to the beginning of the file, which in the end ends up being the absolute position. It is possible to read from the current position and unlike the end of the file.

Understand by position the byte of the file in sequence.

    
17.10.2014 / 20:22