我有一个包含“1234567”的文件(test.txt)。但是,当我尝试使用 FileStream.Read 在 C# 上读取它时,我只得到 0(在这种情况下是七个零)。谁能告诉我为什么?我真的迷路了。
编辑:问题已解决,比较运算符错误。但是现在它返回“49505152535455”
编辑2:完成。作为记录,我必须将byte变量输出为char。
using System;
using System.IO;
class Program
{
static void Main()
{
FileStream fil = null;
try
{
fil = new FileStream("test.txt", FileMode.Open,FileAccess.Read);
byte[] bytes = new byte[fil.Length];
int toRead = (int)fil.Length;
int Read = 0;
while (toRead < 0)
{
int n = fil.Read(bytes, Read, toRead);
Read += n;
toRead -= n;
}
//Tried this, will only return 0000000
foreach (byte b in bytes)
{
Console.Write(b.ToString());
}
}
catch (Exception exc)
{
Console.WriteLine("Oops! {0}", exc.Message);
}
finally
{
fil.Close();
}
Console.ReadLine();
}
}