For my assignment, I have to read a text file one character at a time and display it in the console. Here's my code for this:

class TEXT
{
    static StreamReader reader;
    static void Main()
    {

        int ch, characters = 0;

        // file that is being read in and displayed one character at a time in the console screen.
        reader = new StreamReader("../../../data.txt");
        StreamWriter writer;
        // new file where hex characters will be written to.
        writer = new StreamWriter("../../../s_drcyphert.txt");

        do
        {
            //---------------------------------------
            // read one character at a time.
            ch = reader.Read();
            //----------------------------------------
            // write each char to the console screen
            Console.Write((char)ch);
            //----------------------------------------
            // count the characters
            characters++;
            //--------------------------------------------------
            // create a method, WriteByte() for this task. use parameters to pass in the character
            // to be written and a reference to the output file.
            WriteByte(ch, ref writer);
            //---------------------------------------------------
           

        } while (!reader.EndOfStream);

I have to output the text that was read into a new file using StreamWriter, one character at a time, and I have to output it in hex characters (using the WriteByte() method). Am I calling the method correctly? And if so, what should that method look like and what should be in that method? I've been reading non-stop and can't seem to grasp the ideas.

>Am I calling the method correctly?

Ok! but you can avoid the use of ref argument.

WriteByte(ch,writer);
....
....
void WriteByte(char ch,Writer writer)
{
   ....
}

>Am I calling the method correctly?

Ok! but you can avoid the use of ref argument.

WriteByte(ch,writer);
....
....
void WriteByte(char ch,Writer writer)
{
   ....
}

Sorry....I already figured this one out. I passed the writer by reference so I didn't have to open the writer twice. It only took 1 line of code. lol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.