Двоичные файлы - C#
Формулировка задачи:
Есть такой код, можете, пожалуйста, его переделать для работы уже не с текстовым а с двоичным файлом???
static void Main(string[] args)
{
string line;
// Read the file and display it line by line.
System.IO.StreamReader file = new System.IO.StreamReader(@"E:\Практика. Булгаков\1.txt");
int c = 0;
while ((line = file.ReadLine()) != null)
{
Console.WriteLine(line);
c += line.Length;
}
Console.WriteLine();
Console.Write("Количесвто символов в данном текстовом документе: ");
Console.Write(c);
file.Close();
// Suspend the screen.
System.Console.ReadLine();
}
}
}Решение задачи: «Двоичные файлы»
textual
Листинг программы
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(@"E:\1.bin", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
int countBytes = 0;
byte currByte;
while (br.BaseStream.Position != br.BaseStream.Length)
{
currByte = br.ReadByte();
countBytes++;
Console.Write("{0:X} ", currByte); //hex out
}
br.Close();
fs.Close();
Console.WriteLine();
Console.WriteLine("Количество байт в бинарном файле: {0}", countBytes);
Console.ReadKey();
}
}
}