我的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();
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();
0.123456 0.0 0.0 'aaagggg.sca'
0.12 0.0 0.0 'aaa.sca'
0.00 0.0 0.0 ' '
请大家指点一下如何写code。
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)));
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)));
//从文件中读取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不需要另外定义。
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维数组又该如何做呢?请指点,谢谢啊!