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
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
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());
}
} }
}
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();
}
}
} }
}现在我按照读到的每一个数字显示,不过给你改成静态方法了.