Writing a string to a file throws an exception in C #

I am getting the error "Format FixException: Invalid Input Form" which I do not understand.

I am using the following lines to write a line to a text file:

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

      

(the coding part has to do with the fact that I have an option in my application to set it to utf-8 or iso-8859-1 ... but I think it doesn't matter).

All my lines are writing just fine, except for one line that is different from the others because it actually has a piece of javascript code. I'm sure one of the special characters might be causing the problem, but how do I know?

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

System.Console.WriteLine(mystring);

      

and it wrote to the console just fine - no errors.

reference

Thank! (and happy New Year!)

-Adeena

+1


source to share


2 answers


The overload you are using takes a format as the first parameter and objects to be injected after that.

You can do one of the following:

sw.Write(mystring + Environment.NewLine);

      



or

sw.Write("{0}{1}", mystring, Environment.NewLine);

      

+11


source


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

  • string concatenation
  • call sw.Write

    twice
  • call sw.WriteLine

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



This should be significant if it 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;
        }
  }

      

0


source







All Articles