我的txt格式如下: 
 0.122213   0.0   0.0  'aaahhhhh.sca'
 0.12       0.0   0.0  'aaa.sca'
 0.00       0.0   0.0  ' '
我想不断修改0.12 和 0.122213的值,并将其保存在txt中,我些的代码如下:
//读取Layer数据并保存成数组;
            string layerPath = Application.StartupPath + @"\Layers.txt";
            FileStream fs = new FileStream(layerPath, FileMode.Open, FileAccess.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            string line;
            string[] split;
            ArrayList myAL = new ArrayList();
            while ((line = sr.ReadLine()) != null)
            {
                split = line.Split();
                foreach (string s in split)
                {
                      myAL.Add(s);
                }
            }
            sr.Close();
            fs.Close();            FileStream fs1 = new FileStream(layerPath, FileMode.Open, FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs1);
            //修改指定行列数组的值,并将其保存到txt;
            double a, b;
            string ss = string.Empty;
            for (int i = 0; i <= 10; i++)   //循环改变其值
            {
                a= i/10.0;
                raylHeight = b+ 0.1;
                myAL[1] = raylHeight.ToString();
                myAL[11] = mieHeight.ToString();                //请教如何将myAL中的值按照原先的格式保存到txt中,并且不重复
                   
                //foreach (string str in myAL)
                //{
                    sw.WriteLine(str);
                //}  
            }
            sw.Close();
            fs1.Close();         

解决方案 »

  1.   

    我的txt格式如下:  
     0.123456   0.0   0.0  'aaagggg.sca'
     0.12       0.0   0.0  'aaa.sca'
     0.00       0.0   0.0  ' '
    请大家指点一下如何写code。
      

  2.   

    本帖最后由 caozhy 于 2011-10-24 22:12:15 编辑
      

  3.   

    请教caozhy, x、y 分别代表什么?
      

  4.   

    能否请caozhy将x,y说的再具体点呢? x,y指代的是double a, b吗? 还是s中的某一两个值?请指点,多谢啊!
      

  5.   

    x和y都是string类型。
    x表示一行,y表示一行中的一列。
      

  6.   

    我定义的是string类型,还是有错误,是不是语法有错误呢?s = File.ReadAllText(layerPath);            string x,y;
                var data = (from x in s.Split(new char[] { '\r', '\n' })
                select new { (from y in x.Split(' ') select y).ToList() }).ToList();
                data[1][0] = "0.13";
                s = string.Join("\r\n", data.Select(x => string.Join(" ", x)));
      

  7.   

    s = File.ReadAllText(layerPath);            string x,y;
                var data = (from x in s.Split(new char[] { '\r', '\n' })
                select new { (from y in x.Split(' ') select y).ToList() }).ToList();
                data[1][0] = "0.13";
                s = string.Join("\r\n", data.Select(x => string.Join(" ", x)));
      

  8.   

    修改下
    //从文件中读取s
    string s = @"0.122213 0.0 0.0 'aaahhhhh.sca'
    0.12 0.0 0.0 'aaa.sca'
    0.00 0.0 0.0 ' '
    ";
    var data = (from x in s.Split(new char[] { '\r', '\n' })
               select (from y in x.Split(' ') select y).ToList()).ToList();
    data[1][0] = "0.13";
    s = string.Join("\r\n", data.Select(x => string.Join(" ", x)));
    //把s写回文件x、y不需要另外定义。
      

  9.   

    多谢caozhy的帮忙,再请教一下  
    x => string.Join(" ", x) 指的是什么意思呢?
    2维数组写成:
    var data = (from x in s.Split(new char[] { '\r', '\n' })
               select (from y in x.Split(' ') select y).ToList()).ToList();3维数组又该如何做呢?请指点,谢谢啊!