当前位置:编程学习 > C#/ASP.NET >>

求解。。。将数字写入文件时变成对应的ASCII码了!请问怎么解决》?

求解!!! --------------------编程问答-------------------- 写入的时候传一个编码的参数 --------------------编程问答-------------------- 啊?什么意思》?我是从文本框中输入数字的 --------------------编程问答-------------------- 你把代码贴出来看看吧 --------------------编程问答--------------------

           string str = "7438297402384";
            StreamWriter sw = new StreamWriter("D:\\123.txt", false, Encoding.ASCII);
            for (int i = 0; i < str.Length; i++)
            {
                char ch = str[i];
                int ascii = (int)ch;
                sw.WriteLine(ascii);
            }
            sw.Close();
--------------------编程问答--------------------
引用 4 楼 sp951 的回复:
C# code


           string str = "7438297402384";
            StreamWriter sw = new StreamWriter("D:\\123.txt", false, Encoding.ASCII);
            for (int i = 0; i < str.Length; i++)
        ……


楼上应该理解错了,楼主的意思写入文件的是ASCII吗,希望是正常的字符. --------------------编程问答--------------------   类中的代码:
   public int WriteOneRecord(long num)
        {

            try
            {
                if (file != null && bw != null)
                {
                    file.Seek(num * SIZE, SeekOrigin.Begin);
                    bw = new BinaryWriter(file);
                        foreach (clsSuoYin csy in Arr)
                        {
                            bw.Write(csy.Name);
                            bw.Write(csy.Age);
                           
                        }
                        bw.Close();
                    }
                }

            catch (Exception ex) { string buff = ex.Message; return 0; }
            return 1;
        }
窗体中的代码:
    private void button1_Click(object sender, EventArgs e)
        {
            int age;
                int.TryParse(textBox2.Text, out age);
                asy.add(textBox1.Text, age);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int flag;
            try
            {
                asy.Open(FILENAME);
                recs = asy.GetRecordCount();
                asy.Open(FILENAME);
              flag=asy.WriteOneRecord(recs);
              if (flag == 1) { MessageBox.Show("输入成功","无错误"); }

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            finally { asy.Close(); }
        } --------------------编程问答-------------------- 借用一下4楼的代码:

string str = "7438297402384";
StreamWriter sw = new StreamWriter("D:\\123.txt", true, Encoding.UTF8);
sw.WriteLine(str);            
sw.Close();
--------------------编程问答-------------------- 抱歉!我的代码在4楼!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,