TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ram Prasad
NA
326
18.4k
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?
public
class
Program
{
public
static
string
BNum()
{
string
Val =
""
;
//Lines of Code
byte
[] numbers = Encoding.ASCII.GetString(bytes.ToArray())
//Lines of Code
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()));
//More Code
}
}
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;
}
}
Reply
Answers (
2
)
Read an object variable having “FileInfo” object content.
What is the best way to dispose COM object in C#