手头上有个文件 ,要调用本机硬件上的接口给发送出去 ,现在调试出了问题 ,请各位大侠指点protected void senddata()
{
string path = "D:\\test.t";
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
long mm = file.Length;
int intmm = (int)mm; //取得文件大小
BinaryReader binaryreader = new BinaryReader(file);
byte[] buff = new byte[37600]; //定义一个指定大小的字节数组(37600 bytes)用作缓冲区,将文件流放到缓冲区里面,然后发送出去 s1 = true;
while (s1 && (i = binaryreader.Read(buff, 0, 37600)) != 0)
{
//
binaryreader.Read(buff, 0, 37600);
bst_transmit(buff,...);//此处是我调用接口的发送函数
}
} 现在出现的问题是,我用以上的代码发送 ,buff 只能读到文件流 37600字节 ,然后就将这37600字节的数据循环发送出去了,而我的本意是 把这个文件完整的读下来 然后循环发送出去 ,这该怎么弄 ?
{
string path = "D:\\test.t";
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
long mm = file.Length;
int intmm = (int)mm; //取得文件大小
BinaryReader binaryreader = new BinaryReader(file);
byte[] buff = new byte[37600]; //定义一个指定大小的字节数组(37600 bytes)用作缓冲区,将文件流放到缓冲区里面,然后发送出去 s1 = true;
while (s1 && (i = binaryreader.Read(buff, 0, 37600)) != 0)
{
//
binaryreader.Read(buff, 0, 37600);
bst_transmit(buff,...);//此处是我调用接口的发送函数
}
} 现在出现的问题是,我用以上的代码发送 ,buff 只能读到文件流 37600字节 ,然后就将这37600字节的数据循环发送出去了,而我的本意是 把这个文件完整的读下来 然后循环发送出去 ,这该怎么弄 ?
LZ你每回都是从0开始读37600当然每回都是读同样的数据了。
每次读取之后下次读取的时候,要提升流的位置的
{
string path = "D:\\test.t";
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
long mm = file.Length;
int intmm = (int)mm; //取得文件大小
BinaryReader binaryreader = new BinaryReader(file);
byte[] buff = new byte[37600]; //定义一个指定大小的字节数组(37600 bytes)用作缓冲区,将文件流放到缓冲区里面,然后发送出去 s1 = true;
int count=0;
while (s1 && (i = binaryreader.Read(buff, 0, 37600)) != 0)
{
//
count+=i;
binaryreader.Read(buff, 0, count);
}
bst_transmit(buff,...);//读完以后发送
}
楼主试一下看怎么样!
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
int len = 37600;
byte[] buf = new byte[len];
int getCount = 0;
getCount = fs.Read(buf, 0, buf.Length);
while (getCount > 0)
{
bst_transmit(buff, 0, getCount);//注意这里要用getCount,当然参数是我自定义的,和你的可能不一样
}
fs.Close();
继续读文件37600字节到buff,发送buff中的数据,然后开始下一轮。
应该将循环体中的Read操作去掉才对吧,另外条件s1不知道是用来干什么的
{
string path = "D:\\test.t";
FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read);
long mm = file.Length;
int intmm = (int)mm; //取得文件大小
BinaryReader binaryreader = new BinaryReader(file);
MemoryStream ms = new MemoryStream ();//新建存储为内存的流
byte[] buff = new byte[37600]; //定义一个指定大小的字节数组(37600 bytes)用作缓冲区,将文件流放到缓冲区里面,然后发送出去 s1 = true;
int count=0;
while (s1 && i<intmm)
{
int a = binaryreader.Read(buff, 0, 37600);
count+=a;
ms.Write(buff, 0, a);
bst_transmit(buff,...);//读完以后发送 }
}
加了一个 MemoryStream 后 按上面的代码调试成功