开发者

Read only a specified span

开发者 https://www.devze.com 2023-02-21 18:19 出处:网络
I have a textfile and I want to read only a specified span from it (like position 20 to 80). I\'m using the following code, but it reads from 0 to the length of the span.

I have a textfile and I want to read only a specified span from it (like position 20 to 80).

I'm using the following code, but it reads from 0 to the length of the span.

char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{               
    reader.ReadBlock(buffer, 0, span.Len开发者_如何转开发gth);
}

Can someone help me? Thanks


char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{    
    reader.BaseStream.Seek(span.Start, SeekOrigin.Begin); // or SeekOrigin.Current if you want to loop
    reader.Read(buffer, 0, span.Length);
}

Assumes the type of span has a Start property.


char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{               
    reader.ReadBlock(buffer, startIndex, span.Length);
}

startIndex= from where to start
span.Length = number of char's to read


Have you tried?

char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{               
    reader.ReadBlock(buffer, 20, span.Length);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消