怎么读写C#的double型二维数组到文件
我想用C#读写一个很大的double型矩阵,或者简单点把double型数据写入文件。哪位帮忙下...
我想用C#读写一个很大的double型矩阵,或者简单点把double型数据写入文件。哪位帮忙下
展开
2个回答
展开全部
你看看吧
public static void Main(string[] args)
{
double[,] r = { { 2, 3 }, { 4, 5 } };
//文本方式
using (StreamWriter sw = new StreamWriter("1.txt", false))
{
foreach (double d in r)
{
sw.Write(d);
sw.Write(" ");
}
}
string[] temp = File.ReadAllText("1.txt", Encoding.Default).Split(new char[] { ' ' });
int i = 0;
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = Double.Parse(temp[i]);
i++;
}
Console.WriteLine("");
//二进制方式
using (FileStream fs = new FileStream("1.bin", FileMode.Create, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
foreach (double d in r)
{
bw.Write(d);
}
}
}
using (FileStream fs = new FileStream("1.bin", FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = br.ReadDouble();
}
}
}
}
public static void Main(string[] args)
{
double[,] r = { { 2, 3 }, { 4, 5 } };
//文本方式
using (StreamWriter sw = new StreamWriter("1.txt", false))
{
foreach (double d in r)
{
sw.Write(d);
sw.Write(" ");
}
}
string[] temp = File.ReadAllText("1.txt", Encoding.Default).Split(new char[] { ' ' });
int i = 0;
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = Double.Parse(temp[i]);
i++;
}
Console.WriteLine("");
//二进制方式
using (FileStream fs = new FileStream("1.bin", FileMode.Create, FileAccess.Write))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
foreach (double d in r)
{
bw.Write(d);
}
}
}
using (FileStream fs = new FileStream("1.bin", FileMode.Open, FileAccess.Read))
{
using (BinaryReader br = new BinaryReader(fs))
{
for (int y = 0; y < 2; y++)
for (int x = 0; x < 2; x++)
{
r[y, x] = br.ReadDouble();
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询