Mustafa
Mustafa

Reputation: 177

Reading from a text file error

So I've got a text file called "NEW.txt" and I want to read its contents from the console window. I realize that there is more than one way to skin a cat, but I was trying to implement this one

using (System.IO.StreamReader reader = new System.IO.StreamReader("NEW.txt"))
{
    String content = reader.ReadToEnd();
    Console.WriteLine(content);
}

But I get the error that "An unhandled exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll

Additional information: Cannot write to a closed TextWriter."

What is the TextWriter and why is it closed?

Update:

       //using (System.IO.StreamWriter writer = new System.IO.StreamWriter("NEW.txt"))
       //     {
       //         System.Console.SetOut(writer);
       //         System.Console.WriteLine("Hello text file");
       //         System.Console.WriteLine("I'm writing to you from visual C#");
       //     }

       //This following part only works when the previous block is commented it out

        using (System.IO.StreamReader reader = new System.IO.StreamReader("NEW.txt"))
        {
            string content = reader.ReadToEnd();
            Console.WriteLine(content);

        }

Assuming the problem was the this line "System.Console.SetOut(writer);" How can I change the output stream back to the console window ?

Upvotes: 0

Views: 1017

Answers (1)

Andrew Barber
Andrew Barber

Reputation: 40150

This is your code. You noted that it only works when you comment out the top at the part:

   //using (System.IO.StreamWriter writer = new System.IO.StreamWriter("NEW.txt"))
   //     {
   //         System.Console.SetOut(writer);
   //         System.Console.WriteLine("Hello text file");
   //         System.Console.WriteLine("I'm writing to you from visual C#");
   //     }

   //This following part only works when the previous block is commented it out

    using (System.IO.StreamReader reader = new System.IO.StreamReader("NEW.txt"))
    {
        string content = reader.ReadToEnd();
        Console.WriteLine(content);

    }

Now that you've included the code you were commenting out, I see the problem here... You are setting a StreamWriter as the 'Out' for console. And then you are closing that StreamWriter - closing the associated TextWriter. And then you are trying to use it again later, causing the error: The TextWriter has been closed because you closed it by that code.

To fix this, change your commented code to do this:

   using (System.IO.StreamWriter writer = new System.IO.StreamWriter("NEW.txt"))
        {
            /* This next line is the root of your problem */
            //System.Console.SetOut(writer);

            /* Just write directly with `writer` */
            writer.WriteLine("Hello text file");
            writer.WriteLine("I'm writing to you from visual C#");
        }

There's no need to go through Console here. Just write with the writer directly.

Upvotes: 3

Related Questions