内容简介:代码日志版权声明:翻译自:http://stackoverflow.com/questions/1562417/read-binary-data-from-console-in
有没有办法从C#中的stdin读取二进制数据?
在我的问题我有一个程序启动并接收stdin二进制数据.
基本上:
C:> myImageReader<someImage.jpg 我想写一个程序,如:
static class Program
{
static void Main()
{
Image img = new Bitmap(Console.In);
ShowImage(img);
}
}
但是Console.In不是Stream,它是一个TextReader.
(如果我尝试读取char [],TextReader解释数据,不允许我访问原始字节.)
任何人都知道如何访问实际的二进制输入?
干杯,
雷夫
要读取二进制文件,最好的方法是使用原始输入流 – 这里显示的是stdin和stdout之间的“echo”
using (Stream stdin = Console.OpenStandardInput())
using (Stream stdout = Console.OpenStandardOutput())
{
byte[] buffer = new byte[2048];
int bytes;
while ((bytes = stdin.Read(buffer, 0, buffer.Length)) > 0) {
stdout.Write(buffer, 0, bytes);
}
}
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/1562417/read-binary-data-from-console-in
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。