C# Write to File Problem

noobman

[H]ard|Gawd
Joined
Oct 15, 2005
Messages
1,475
Code:
//snip
            StreamReader inputFile = File.OpenText(args[1]);
            StreamWriter outputFile = new StreamWriter(args[2]);
            List<string> masterList = new List<string>();
            string input;

            input = inputFile.ReadLine();
            while (input != null)
            {
                if (masterList.Contains(input))
                {
                    input = inputFile.ReadLine();
                    continue;
                }
                
                
                outputFile.WriteLine(input);
                masterList.Add(input);
                Console.WriteLine(input);
                input = inputFile.ReadLine();
            }
I'm mystified as to why this doesn't work. The console output is correct... but the content never gets written to file. The file gets created, but remains blank. I used very similar code for another quick n dirty app (not allowed to install Python on our servers or else I'd use that) and it seemed to work like a charm.

What gives?
 
We'd need to see more of the code to know what's going on. Is this a program that stays constantly running, or does it terminate after running this code? Either way, you could try flushnig the StreamWriter.
 
We'd need to see more of the code to know what's going on. Is this a program that stays constantly running, or does it terminate after running this code? Either way, you could try flushnig the StreamWriter.

That did the trick!

What you're seeing is more or less the entire source code... it's a one-time run application. Like I said, if I had access to Python I'd probably use that, or some other scripting language.

Ahh, the perils of working in IT in a non-high tech company. I sure do miss that environment :(
 
The MSDN documentation for StreamWriter doesn't flush it in its code example, so it's easy to assume that the destructor automatically flushes for you....quite interesting, regardless, and probably worth more investigation or at least filing a complaint against the documentation.
 
Back
Top