想手动输入字节并输出文件 [英] Want to manually input bytes and make output file

查看:62
本文介绍了想手动输入字节并输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<pre>//The following piece of code takes an input file, read it, output binary to decimal conversion to the screen, and re-write the the file BYTE BYTE to another location.
//For this purpose, it works great. However I would like down this process into two (2) separate independent programs.

//I would like to create a separate program that takes a MANUAL INPUT of all BYTES READ (assume that i have them all written down on a piece of paper), one after the other, and re-create the file read to another location.
//This separate program will not read in ANYTHING. All it will do is take the manual input of bytes and re-write the file to another specified location.

using System;
using System.IO;
using System.Collections;

namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Fol");
            if (!da.Exists)
            {
                Console.WriteLine("The folder '{0}' does not exist.", da.FullName);
                return;
            }
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                Console.WriteLine("There are no files in the folder '{0}'.", da.FullName);
                return;
            }
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);

            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];

            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));

            using (Stream input = File.OpenRead(filePath))
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bytesRead;
                while ((bytesRead = input.Read(buffer, 0, BufferSize)) > 0)
                {
                    for (int count = 0; count < bytesRead; count++)
                    {
                        byte theByte = buffer[count];
                        string theByteInBinary = Convert.ToString(theByte, 2).PadLeft(8, '0');
                        Console.WriteLine("{0} = {1}, Count={2}", theByteInBinary, theByte, count);
                    }
                    output.Write(buffer, 0, bytesRead);
                }
            }
        }
    }
}







我尝试了什么:



我试过评论:




What I have tried:

I have tried commenting out the:

using (Stream input = File.OpenRead(filePath))



但我在其他地方遇到错误,我不知道如何修复。代码编译并运行良好,但现在我真的需要一种手动输入字节的方法并允许:


But I get error in other places that I don't know how to fix. The code compile and works great, but now i really need is a way to enter the bytes manually and allow:

output.Write(buffer, 0, bytesRead);

重新制作文件。

推荐答案

您的导师需要做的是从文件中读取 - 您的输入流 - 直接从用户那里阅读数据。



要做到这一点,你需要摆脱这条线:

What your tutor needs you do do is change from reading from a file - your input Stream - to reading the data directly from the user.

To do that, you need to get rid of the line:
using (Stream input = File.OpenRead(filePath))



并将代码中的所有引用替换为 input 引用 Console 类,并使用各种Read方法(Read和/或ReadLine)从用户那里获取字节。

您需要决定用户输入字节的方式(他不能只键入它们,因为它只限于键盘中可用的字符,而字节值范围从0到255)然后决定你如何告诉你有没有更多等等。



坐下来思考一下这个任务,以及你在上一课中学到了什么 - 如果你注意了它应该很简单!


And replace all references in your code to input with references to the Console class, and use the various Read methods (Read and / or ReadLine) to fetch the bytes from the user.
You will need to decide how the user enters the bytes (he can't just type them as he would be restricted to just the characters available from the keyboard while byte values range from 0 to 255) then decide how you tells you there are no more and so forth.

Sit down and think about the task, and what you learned in the last lesson - it should be pretty simple if you paid attention!


这篇关于想手动输入字节并输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆