Ram Prasad

Ram Prasad

  • NA
  • 326
  • 16.8k

Input to Method instead of Console.writeline

May 22 2020 2:24 AM
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?
  1. public class Program  
  2. {  
  3. public static string BNum()  
  4. {  
  5. string Val = "";  
  6. //Lines of Code  
  7. byte[] numbers = Encoding.ASCII.GetString(bytes.ToArray())  
  8. //Lines of Code  
  9. return Val;  
  10. }  
  11.   
  12. public static void Main(string[] args)  
  13. {  
  14. const int chunkSize = 64;  
  15. foreach (IEnumerable<byte> bytes in ReadByChunk(chunkSize))  
  16. {  
  17. Console.WriteLine("==========================================");  
  18. Console.WriteLine(Encoding.ASCII.GetString(bytes.ToArray()));  
  19. //More Code  
  20. }  
  21. }  
  22. public static IEnumerable<IEnumerable<byte>> ReadByChunk(int chunkSize)  
  23. {  
  24. IEnumerable<byte> result; int startingByte = 0;  
  25. do  
  26. {  
  27. result = ReadBytes(startingByte, chunkSize);  
  28. startingByte += chunkSize; yield return result;  
  29. }  
  30. while (result.Any());  
  31. }  
  32. public static IEnumerable<byte> ReadBytes(int startingByte, int byteToRead)  
  33. {  
  34. byte[] result;  
  35. using (FileStream stream = File.Open(@<path>, FileMode.Open, FileAccess.Read, FileShare.Read))  
  36. using (BinaryReader reader = new BinaryReader(stream))  
  37. {  
  38. int bytesToRead = Math.Max(Math.Min(byteToRead, (int)reader.BaseStream.Length - startingByte), 0);  
  39. reader.BaseStream.Seek(startingByte, SeekOrigin.Begin);  
  40. result = reader.ReadBytes(bytesToRead);  
  41. }  
  42. return result;  
  43. }  
  44. }  

Answers (2)