Hello,
I am dividing a large file into smaller chunks using the code shown below.
Instead of showing it as an output in Console.writeline
I would like the result to used as input to BNum() Method. How can this be done. Please advise?
- public class Program
- {
- public static string BNum()
- {
- string Val = "";
-
- byte[] numbers = Encoding.ASCII.GetString(bytes.ToArray())
-
- return Val;
- }
-
- public static void Main(string[] args)
- {
- const int chunkSize = 64;
- foreach (IEnumerable<byte> bytes in ReadByChunk(chunkSize))
- {
- Console.WriteLine("==========================================");
- Console.WriteLine(Encoding.ASCII.GetString(bytes.ToArray()));
-
- }
- }
- public static IEnumerable<IEnumerable<byte>> ReadByChunk(int chunkSize)
- {
- IEnumerable<byte> result; int startingByte = 0;
- do
- {
- result = ReadBytes(startingByte, chunkSize);
- startingByte += chunkSize; yield return result;
- }
- while (result.Any());
- }
- public static IEnumerable<byte> ReadBytes(int startingByte, int byteToRead)
- {
- byte[] result;
- using (FileStream stream = File.Open(@<path>, FileMode.Open, FileAccess.Read, FileShare.Read))
- using (BinaryReader reader = new BinaryReader(stream))
- {
- int bytesToRead = Math.Max(Math.Min(byteToRead, (int)reader.BaseStream.Length - startingByte), 0);
- reader.BaseStream.Seek(startingByte, SeekOrigin.Begin);
- result = reader.ReadBytes(bytesToRead);
- }
- return result;
- }
- }