当前位置: 首页>>代码示例>>C#>>正文


C# BinaryReader.ReadByte方法代码示例

本文整理汇总了C#中System.IO.BinaryReader.ReadByte方法的典型用法代码示例。如果您正苦于以下问题:C# BinaryReader.ReadByte方法的具体用法?C# BinaryReader.ReadByte怎么用?C# BinaryReader.ReadByte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.BinaryReader的用法示例。


在下文中一共展示了BinaryReader.ReadByte方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.IO;

class BinaryRW
{
    static void Main()
    {
        int i = 0;

        // Create random data to write to the stream.
        byte[] writeArray = new byte[1000];
        new Random().NextBytes(writeArray);

        BinaryWriter binWriter = new BinaryWriter(new MemoryStream());
        BinaryReader binReader = 
            new BinaryReader(binWriter.BaseStream);

        try
        {
            // Write the data to the stream.
            Console.WriteLine("Writing the data.");
            for(i = 0; i < writeArray.Length; i++)
            {
                binWriter.Write(writeArray[i]);
            }

            // Set the stream position to the beginning of the stream.
            binReader.BaseStream.Position = 0;

            // Read and verify the data from the stream.
            for(i = 0; i < writeArray.Length; i++)
            {
                if(binReader.ReadByte() != writeArray[i])
                {
                    Console.WriteLine("Error writing the data.");
                    return;
                }
            }
            Console.WriteLine("The data was written and verified.");
        }

        // Catch the EndOfStreamException and write an error message.
        catch(EndOfStreamException e)
        {
            Console.WriteLine("Error writing the data.\n{0}",
                e.GetType().Name);
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:50,代码来源:BinaryReader.ReadByte

示例2: BinaryReader.ReadByte()

//引入命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

class Program {
    static void Main(string[] args) {
        FileInfo f = new FileInfo("BinFile.dat");
        BinaryWriter bw = new BinaryWriter(f.OpenWrite());

        Console.WriteLine("Base stream is: {0}", bw.BaseStream);

        double aDouble = 1234.67;
        int anInt = 34567;
        char[] aCharArray = { 'A', 'B', 'C' };

        bw.Write(aDouble);
        bw.Write(anInt);
        bw.Write(aCharArray);
        bw.Close();

        BinaryReader br = new BinaryReader(f.OpenRead());
        int temp = 0;
        while (br.PeekChar() != -1) {
            Console.Write("{0,7:x} ", br.ReadByte());
            if (++temp == 4) {
                Console.WriteLine();
                temp = 0;
            }
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:33,代码来源:BinaryReader.ReadByte


注:本文中的System.IO.BinaryReader.ReadByte方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。