Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中,将字符串写入文件会导致异常_C#_String - Fatal编程技术网

C# 在C语言中,将字符串写入文件会导致异常

C# 在C语言中,将字符串写入文件会导致异常,c#,string,C#,String,我收到一个FormatException:输入字符串的格式错误不正确,我不理解 我使用以下行将字符串写入文本文件: using (StreamWriter sw = new StreamWriter(myfilename, false, System.Text.Encoding.GetEncoding(enc))) { sw.Write(mystring, Environment.NewLine); } 编码部分是因为我的应用程序中有一个选项可以将其设置为utf-8或iso-8859-

我收到一个FormatException:输入字符串的格式错误不正确,我不理解

我使用以下行将字符串写入文本文件:

using (StreamWriter sw = new StreamWriter(myfilename, false, System.Text.Encoding.GetEncoding(enc)))
{
    sw.Write(mystring, Environment.NewLine);
}
编码部分是因为我的应用程序中有一个选项可以将其设置为utf-8或iso-8859-1。。。但我认为这无关紧要

我所有的字符串都写得很好,除了这个字符串与其他字符串不同,因为它实际上包含一段javascript代码。我确信其中一个特殊的角色可能导致了这个问题,但我怎么知道呢

我尝试的一件事是在上面的sw.Write语句之前插入以下行:

System.Console.WriteLine(mystring);
它向控制台发出的信息很好——没有错误

帮忙

谢谢!新年快乐


-Adeena

您使用的重载将格式作为第一个参数,然后再注入对象

您可以执行以下任一操作:

sw.Write(mystring + Environment.NewLine);


您正在使用的重载将格式作为第一个参数,然后再注入对象

您可以执行以下任一操作:

sw.Write(mystring + Environment.NewLine);


为了回应DK的评论,我测试了扩展字符串连接的速度。我用三个选项进行了设置

连接字符串 呼叫sw。写两次 呼叫sw.WriteLine 在我的机器上,第二个选项比平均速度快88%。在10000000次迭代中,它们使用35172420和3385毫秒

只有当这是在程序中多次调用的代码时,它才有意义

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;
        }
  }

为了回应DK的评论,我测试了扩展字符串连接的速度。我用三个选项进行了设置

连接字符串 呼叫sw。写两次 呼叫sw.WriteLine 在我的机器上,第二个选项比平均速度快88%。在10000000次迭代中,它们使用35172420和3385毫秒

只有当这是在程序中多次调用的代码时,它才有意义

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;
        }
  }

以上两个样本都有效;sw.WriteLinemystring也将如此。然而,除非清楚错误的原因,否则问题还没有解决,只是避免了;啊,还有一件小事——字符串连接是一个坏习惯;sw.WriteLinemystring也将如此。然而,除非清楚错误的原因,否则问题还没有解决,只是避免了;啊,还有一件小事——字符串连接是个坏习惯。