views:

614

answers:

2

I'm getting a "FormatException: Input string was not in a correct format" error that I don't understand.

I'm using the following lines to write a string to a text file:

using (StreamWriter sw = new StreamWriter(myfilename, false, System.Text.Encoding.GetEncoding(enc)))
{
    sw.Write(mystring, Environment.NewLine);
}

(the encoding part is because I do have an option in my application to set it to utf-8 or iso-8859-1... but I think that's irrelevant).

All of my strings write out just fine except this one string that is different from the others because it actually has a snippet of javascript code in it. I'm sure that one of the special characters there might be causing the problem but how do I know?

The one thing I tried was to insert the following line just before the sw.Write statement above:

System.Console.WriteLine(mystring);

and it wrote out to the console just fine - no error.

Help?

Thanks! (and Happy New Year!)

-Adeena

+10  A: 

The overload you are using takes the format as the first parameter, and objects to inject after that.

You can do either of the following:

sw.Write(mystring + Environment.NewLine);

or

sw.Write("{0}{1}", mystring, Environment.NewLine);
Øyvind Skaar
That worked, thanks! (the first statement with the "+")
adeena
Both samples above will work; so will sw.WriteLine(mystring). However, unless it's clear "why" there was an error, the problem is not resolved yet, just avoided ;)
DK
Ah, and another little thing - string concatenation is a bad habit.
DK
A: 

In response to the comments from DK, I tested to what extend string concatenation is slower. I made this setup with three options;

  • concatenating the string
  • calling sw.Write twice
  • calling sw.WriteLine

On my machine, the second option is about 88% faster than average. At 10000000 iterations they use 3517, 2420 and 3385 milliseconds.

It should only be significant if this is code that is called many times in your program.

using System;
using System.IO;
using System.Text;

class Program
    {
        static void Main(string[] args)
        {
            const string myString = "kdhlkhldhcøehdhkjehdkhekdhk";
            int iterations=getIntFromParams(args, 0, 10);
            int method = getIntFromParams(args, 1, 0);
            var fileName=Path.GetTempFileName();
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.Default))
            {
                switch (method)
                {
                    case 0:

                        Console.WriteLine("Starting method with concatenation. Iterations: " + iterations);
                        var start0 = DateTimeOffset.Now;
                        for (int i = 0; i < iterations; i++)
                        {
                            sw.Write(myString + Environment.NewLine);
                        }
                        var time0 = DateTimeOffset.Now - start0;
                        Console.WriteLine("End at " + time0.TotalMilliseconds + " ms.");

                        break;
                    case 1:
                        Console.WriteLine("Starting method without concatenation. Iterations: " + iterations);
                        var start1 = DateTimeOffset.Now;
                        for (int i = 0; i < iterations; i++)
                        {
                            sw.Write(myString);
                            sw.Write(Environment.NewLine);
                        }
                        var time1 = DateTimeOffset.Now - start1;
                        Console.WriteLine("End at " + time1.TotalMilliseconds + " ms.");
                        break;
                    case 2:
                        Console.WriteLine("Starting method without concatenation, using WriteLine. Iterations: " + iterations);
                        var start2 = DateTimeOffset.Now;
                        for (int i = 0; i < iterations; i++)
                        {
                            sw.WriteLine(myString);
                        }
                        var time2 = DateTimeOffset.Now - start2;
                        Console.WriteLine("End at " + time2.TotalMilliseconds + " ms.");
                        break;
                }
            }
        }

        private static int getIntFromParams(string[] args, int index, int @default)
        {
            int value;
            try
            {
                if (!int.TryParse(args[index], out value))
                {
                    value = @default;
                }
            }
            catch(IndexOutOfRangeException)
            {
                value = @default;
            }
            return value;
        }
  }
Øyvind Skaar