大家帮忙看下这段代码

cgt86527 2009-04-21 04:25:23
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace 题目2
{
class Program
{
static void Main(string[] args)
{
string m_path = @"c:\datafile3.dat";
int[,] m_array ={ { 2, 3, }, { 4, 5 } };
Program ps = new Program();
//ps.write(m_path, m_array);
ps.read(m_path);


}
public void write(string strpath,int[,] array)
{

FileStream myarray=new FileStream(strpath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(myarray);
foreach (int i in array)
{
bw.Write(i);
}
Console.WriteLine("write succeed");
Console.ReadLine();

}

public void read(string strpath)
{
FileStream m_fa = new FileStream(strpath, FileMode.Open, FileAccess.ReadWrite);
BinaryReader bs = new BinaryReader(m_fa);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
Console.Write(bs.ReadInt32());
}
}

}
}
大家看下我这个write的函数能不能实现把这个矩阵写入的目的,另外为什么我调用我的read函数时无法看到我预先写入文件的矩阵,它给我的是Unhandled Exception:System.IO.EndOfStreamException:Unable to read beyond the end of the stream
...全文
114 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qldsrx 2009-04-21
  • 打赏
  • 举报
回复
很明显超出了长度,我建议你先输出下bs的长度看看吧,写的时候是写入的地址,因为你没用嵌套的循环,就循环了一次,写入了一维数组的首地址。读取的时候你想读取元素,结果就超出数组长度了,其实数组长度为2.
yafeya 2009-04-21
  • 打赏
  • 举报
回复

using System;
using System.IO;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string m_path = @"c:\datafile3.dat";
int[,] m_array = { { 2, 3, }, { 4, 5 } };
write(m_path, m_array);
read(m_path);
}
public static void write(string strpath, int[,] array)
{
using (FileStream myarray = new FileStream(strpath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
BinaryWriter bw = new BinaryWriter(myarray);
foreach (int i in array)
{
bw.Write(i);
}
bw.Flush();
bw.Close();
myarray.Close();
Console.WriteLine("write succeed");
Console.ReadLine();
}
}

public static void read(string strpath)
{
using (FileStream m_fa = new FileStream(strpath, FileMode.Open, FileAccess.ReadWrite))
{
BinaryReader bs = new BinaryReader(m_fa);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
Console.Write(bs.ReadInt32());
Console.ReadLine();
}
}
}

}
}

现在我按照读到的每一个数字显示,不过给你改成静态方法了.
ericzhangbo1982111 2009-04-21
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace 题目2
{
class Program
{
static void Main(string[] args)
{
string m_path = @"c:\datafile3.dat";
int[,] m_array ={ { 2, 3, }, { 4, 5 } };
Program ps = new Program();
ps.write(m_path, m_array);
ps.read(m_path);


}
public void write(string strpath,int[,] array)
{

FileStream myarray=new FileStream(strpath,FileMode.OpenOrCreate,FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(myarray);
foreach (int i in array)
{
bw.Write(i);
}
bw.Close();
myarray.Close();

Console.WriteLine("write succeed");
Console.ReadLine();

}

public void read(string strpath)
{
FileStream m_fa = new FileStream(strpath, FileMode.Open, FileAccess.ReadWrite);
BinaryReader bs = new BinaryReader(m_fa);
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
{
Console.Write(bs.ReadInt32());
}
}

}
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧