日期:2014-05-20  浏览次数:21239 次

关于c# 按字符读取文本文件的问题
想弄一个类似于读书的软件,需要从文本文件中,一次读出固定数量的字符,
需要怎么做啊,找来找去都找不到的说~谢谢了

------解决方案--------------------
From MSDN
既然用的是 ms 的东西,msdn这么好的资料库都不用?

.NET Framework 类库

StreamReader.Read 方法 [C#]请参见
StreamReader 类 | StreamReader 成员 | System.IO 命名空间 | C++ 托管扩展编程
语言
C#

C++

JScript

Visual Basic

全部显示
读取输入流中的下一个字符或下一组字符。

重载列表
读取输入流中的下一个字符并使该字符的位置提升一个字符。

受 .NET Framework 精简版的支持。

[Visual Basic] Overloads Overrides Public Function Read() As Integer
[C#] public override int Read();
[C++] public: int Read();
[JScript] public override function Read() : int;
从 index 开始,从当前流中将最多的 count 个字符读入 buffer。

受 .NET Framework 精简版的支持。

[Visual Basic] Overloads Overrides Public Function Read(Char(), Integer, Integer) As Integer
[C#] public override int Read(char[], int, int);
[C++] public: int Read(__wchar_t __gc[], int, int);
[JScript] public override function Read(Char[], int, int) : int;
示例
[Visual Basic, C#, C++] 下面的示例一次读取五个字符直至达到文件的末尾。

[Visual Basic, C#, C++] 注意 此示例显示如何使用 Read 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Imports System
Imports System.IO
Imports System.Text

Public Class Test

Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt "

Try
If File.Exists(path) Then
File.Delete(path)
End If

Dim sw As StreamWriter = New StreamWriter(path)
sw.WriteLine( "This ")
sw.WriteLine( "is some text ")
sw.WriteLine( "to test ")
sw.WriteLine( "Reading ")
sw.Close()

Dim sr As StreamReader = New StreamReader(path)

Do While sr.Peek() > = 0
'This is an arbitrary size for this example.
Dim c(5) As Char
sr.Read(c, 0, c.Length)
'The output will look odd, because
'only five characters are read at a time.
Console.WriteLine(c)
Loop
sr.Close()
Catch e As Exception
Console.WriteLine( "The process failed: {0} ", e.ToString())
End Try
End Sub
End Class

[C#]
using System;
using System.IO;

class Test
{

public static void Main()
{
string path = @ "c:\temp\MyTest.txt ";

try
{
if (File.Exists(path))
{
File.Delete(path);
}

using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine( "This ");
sw.WriteLine( "is some text ");
sw.WriteLine( "to test ");
sw.WriteLine( "Reading ");
}

using (StreamReader sr = new StreamReader(path))
{
//This is an arbitrary size for this example.
char[] c = null;

while (sr.Peek() > = 0)
{
c = new char[5];
sr.Read(c, 0, c.Length);
//The output will look odd, because
//only five characters are read at a time.
Console.WriteLine(c);