0

I have written a console application that in itself is working as I would like it to. It's main output works great in console. But the results inside the loop I want written to a text file. I have used StreamWriter to attempt this and although I receive no errors on compilation or running, the file in my C: drive remains blank. Can anyone spot any stupid or quick things I have missed?

If you can help, thank you in advance.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\Test.txt");
            double z = 0;
            double x = 1;
            double y = 1;

            Console.WriteLine("How many lines should be written to the file?");
            Console.WriteLine();
            z = double.Parse(System.Console.ReadLine());

            Console.WriteLine("Writing " + z + "lines to file!");
            Console.WriteLine();

            while (z > 0)
            {
                y = Math.Pow(x, 2);
                Console.WriteLine(x + ", " + y*10);
                file.WriteLine(x + ", " + y*10);
                z = z - 1;
                x = x + 1;

            }
            Console.WriteLine();
            Console.WriteLine("**Generation Complete**");
            Console.WriteLine();
            Console.WriteLine("-------------------------------");
            Console.WriteLine();
            Console.WriteLine("**Writing to file successful**");
            Console.WriteLine();
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();

            file.Close();

        }
    }
}
Keith Thomson
  • 63
  • 4
  • 9
  • @DaveDev, Jon Skeet and others. Thanks for the replies. At this point in time, I'm sure it's not a permissions issue as the file is being created. But when I open, it is blank. This includes using the 'using' statement. Any ideas? **Ignore. Working fine now. Thank you so much everyone** – Keith Thomson Oct 29 '13 at 16:27
  • 1
    Any idea what was wrong? – CDspace Oct 29 '13 at 17:26

3 Answers3

2

I've had issues similar to this before. If I recall correctly, the solution came down to wrapping the StreamWriter in a using block, rather than creating it and then trying to close it, e.g.

    static void Main(string[] args)
    {
        using (var file = new System.IO.StreamWriter("c:\\Test.txt"))
        {
        double z = 0;
        double x = 1;
        double y = 1;

        Console.WriteLine("How many lines should be written to the file?");
        Console.WriteLine();
        z = double.Parse(System.Console.ReadLine());

        Console.WriteLine("Writing " + z + "lines to file!");
        Console.WriteLine();

        while (z > 0)
        {
            y = Math.Pow(x, 2);
            Console.WriteLine(x + ", " + y*10);
            file.WriteLine(x + ", " + y*10);
            z = z - 1;
            x = x + 1;

        }
        Console.WriteLine();
        Console.WriteLine("**Generation Complete**");
        Console.WriteLine();
        Console.WriteLine("-------------------------------");
        Console.WriteLine();
        Console.WriteLine("**Writing to file successful**");
        Console.WriteLine();
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();

        }

    }
DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • 1
    If the code is running without exceptions, then adding a `using` statement wouldn't help. It's still a good idea, but it doesn't explain why they OP was seeing a blank file, IMO. – Jon Skeet Oct 29 '13 at 16:49
2

Use the using statament for everything that implements IDisposable like the StreamWriter. This will ensure that unmanaged resources are disposed (even on error). It'll also flush the stream if it's AutoFlush property is false(default).

using(var file = new System.IO.StreamWriter("c:\\Test.txt"))
{
    // ...
    file.WriteLine(x + ", " + y*10);
    // rest of code here ...
}

Does Stream.Dispose always call Stream.Close (and Stream.Flush)

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
2

When I run that code, I see an exception:

Unhandled Exception: System.UnauthorizedAccessException:
Access to the path 'c:\Test.txt' is denied.

It's not clear how you're running this, but if it's configured as a console app and you run it from a console window so you'll definitely see any exceptions, I suspect you'll see the same thing.

Try changing it to write to somewhere you definitely have access to - and also try to work out why you didn't see the exception before.

Additionally it would definitely be better to use a using statement as other answers have shown - but that wouldn't explain a clean run (no exceptions) without the file being created.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194