只读指定范围

发布于 2024-10-28 03:40:36 字数 282 浏览 1 评论 0原文

我有一个文本文件,我只想从中读取指定的范围(例如位置 20 到 80)。

我使用以下代码,但它读取从 0 到跨度的长度。

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

有人可以帮助我吗?谢谢

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.Length);
}

Can someone help me? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-11-04 03:40:37
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);
}

假设 span 的类型具有 Start 属性。

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.

温柔少女心 2024-11-04 03:40:37
char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{               
    reader.ReadBlock(buffer, startIndex, span.Length);
}

startIndex=从哪里开始
span.Length = 要读取的字符数

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

猫腻 2024-11-04 03:40:37

你尝试过吗?

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

Have you tried?

char[] buffer = new char[span.Length];
using (StreamReader reader = new StreamReader(filename))
{               
    reader.ReadBlock(buffer, 20, span.Length);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文