Click here to Skip to main content
15,890,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<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);
to remake the file.
Posted
Updated 22-Nov-17 20:28pm

1 solution

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))

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!
 
Share this answer
 
Comments
computerpublic 23-Nov-17 7:19am    
All you are doing is saying what I said I did. I did comment out "using (Stream input = File.OpenRead(filePath))" and it created problems in other parts of the code. I said I did this when I first ask the question.
OriginalGriff 23-Nov-17 7:46am    
Yes - and I told you what to do to replace it.
I can't see your "modified code" so I can't comment on "error in other places that I don't know how to fix" because I have no idea what the errors are! :laugh:

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
computerpublic 23-Nov-17 15:27pm    
Please let me first say that I am here looking for help. Once i comment out "using (Stream input = File.OpenRead(filePath))", it nullifies other variables which are dependent to the line being commented out. So what I end up with, are other variables which also need to be commenting out and the problem snow ball in other problems. All I want to do is have someone show me how to redo the structure where i am still doing "output.Write(buffer, 0, bytesRead);", but taking the input manually one byte of a time (assume that i have them all the bytes written down on a piece of paper). I no longer want to do the "Read in from file" to get the bytes, but I want to build back the file from all the bytes entered. I am not a professional programmer like the people in this forum. I know my screen cannot be seen, but the entire code is here to be ran. It's only a few short line of code. I do not understand how to fix this problem. If you genuinely know how to fix this problem, please help me. Thank you.
OriginalGriff 24-Nov-17 3:41am    
We understand you are a beginner - but you have to understand that to ever be more than a beginner you need to do this yourself. Us "giving you the solution" helps no-one in the long run, as you don't learn anything about the thought processes involved from handing in my code as your homework! And that means the next - harder - task he sets is even worse for you, because he expects you to have a vague idea how to think like a developer from the work you had to do on this task.

Just removing the line does nothing good on it's own, of course it causes more problems! It's like taking the engine out of your car - it reduces the weight a lot, yes: but it doesn't make it go faster! To do that, you have to put a new engine in, and deal with all the problems that causes with hooking everything up so it runs well.

So stop whining and start thinking:
1) How do I enter a single byte?
2) How can I store a collection of single bytes so I can access them later?
3) How do I signal "no more bytes"?

Read your course notes and I'm pretty sure most of this will have been covered...
computerpublic 24-Nov-17 12:43pm    
OriginalGriff thank very much for your assistance. Can someone in this forum please assist? I don't it is being understand that nothing i do works. I have been at this for weeks. I am totally going in circles and very confused about how the syntax work. It's not for a lack of trying. I have tried so many different ways to succeed and end up with nothing.

I am using a loop with the variable "Totbyte" from the original program to control the loop that will start and terminate the process after the exact amount of bytes as been entered. I also want to use "output.Write(buffer, 0, bytesRead);" to assemble back the file. I am having two(2) problems.

1. How to correct nullified variables that as been commented out.
2. How to enter the new byte with respect to "output.Write(buffer, 0, bytesRead);"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900