读文件,发现如下错误,怎么搞?!帮忙!!大大们来看看!!

fuyiyong 2007-07-12 04:22:57
我读的是一个EXCEL文件
if (reader.PeekChar() == -1)
判断读没读完。。。
就这儿,挂了。。。

----------------------------------------------
The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.
Parameter name: chars

----------------------------------------------
一直好好的,今天突然出现这个错误。。。。
...全文
282 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
fuyiyong 2007-07-13
  • 打赏
  • 举报
回复
算了,不用PeekChar这个方法了,这好像是.net的一个Bug!

改成这样的判断: reader.BaseStream.Position == reader.BaseStream.Length
fuyiyong 2007-07-13
  • 打赏
  • 举报
回复
应该不是编码的问题,看看微软的BCL team怎么说:
------------------------------
Matty4242 :(一个和我出现同样问题的人)
I thought a BinaryReader would read byte by byte without regard for what is in each byte or its significance in the stream and so, with the "ReadByte" method, I would have thought encoding is irrelevant. Perhaps encoding is important for "ReadChar" but I don't see its relevance for "ReadByte", hope you understand? In other words, I simply want to read a byte at a time and get the 8 bit value into my program as a byte data type - is this not possible?
-------------------------------
Lakshan Fernando (BCL Team)
You are right, ReadByte() will work without any encoding considerations. The problem is with PeekChar(). This method will try to peek at a char and can get into an error state.
-------------------------------


fengyecsdn 2007-07-12
  • 打赏
  • 举报
回复
PeekChar()
不同的编码的CHAR是不同的
长度也不同 你读的数据可能出现解码错误
首先要确定编码 再去读

Jinglecat 2007-07-12
  • 打赏
  • 举报
回复
BinaryReader.PeekChar fails when checking for end of file
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1827525&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=406705&SiteID=1
fuyiyong 2007-07-12
  • 打赏
  • 举报
回复
vainnetwork()

很奇怪的问题,本来是一个EXCEL的模板,根据数据的不同生成多个EXCEL文件
问题是,同一批生成的多的文件,有的可以读,有的就不能读。。。。。晕死了!!
我认为是写进去的数据的问题,但写进去的数据就是一些文字处理啊。。。。。。。。。

我一个一个试,发现只要把某几个字去掉就可以读了。。。。天啊。。。。不活了。。。
MicroSoftor 2007-07-12
  • 打赏
  • 举报
回复
看看是不是没传过来,自己断点调试
vainnetwork 2007-07-12
  • 打赏
  • 举报
回复
看提示应该是编码问题,楼主贴详细点代码看看。
不知道是否跟文件编码有关,也可以试试其他文件有无问题!!!
fuyiyong 2007-07-12
  • 打赏
  • 举报
回复
BearRui(开心熊 | 第1个JAVA项目终于完成)

我找到这篇文章的,可看不出什么头绪啊!!
guoweijun394 2007-07-12
  • 打赏
  • 举报
回复
学习了
BearRui 2007-07-12
  • 打赏
  • 举报
回复
楼主看看这个:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=127647&SiteID=1
fuyiyong 2007-07-12
  • 打赏
  • 举报
回复
贴代码太多了,也没有重点,主要就是if (reader.PeekChar() == -1)这一句
我读的是EXCEL文件,我感觉和编码没关系,我先读到byte数组中,然后再根据条件来选择编码

protected MemoryStream stream;
protected BinaryReader reader;
int ContinuedIndex = 0;
public string ReadString(int lengthbits)
{
if (reader.PeekChar() == -1)//NND,就这里错了。。。。
{
if (ContinuedIndex < ContinuedRecords.Count)
{
SwitchToContinuedRecord();
}
else
{
return null;
}
}
int stringlength = lengthbits == 8 ? reader.ReadByte() : reader.ReadUInt16();
byte option = reader.ReadByte();
Encoding encoding = Encoding.ASCII;
int bytelength = stringlength;
if ((option & 0x01) == 1)
{
encoding = Encoding.Unicode;
bytelength *= 2;
}
bool phonetic = (option & 0x04) == 0x04;
bool richtext = (option & 0x08) == 0x08;
int runs = 0;
int size = 0;
if (richtext)
{
runs = reader.ReadUInt16();
}
if (phonetic)
{
size = reader.ReadInt32();
}
byte[] bytes = reader.ReadBytes(bytelength);
if (bytes.Length < bytelength)
{
string firstpart = encoding.GetString(bytes);
SwitchToContinuedRecord();
return firstpart + ReadContinuedString(stringlength - firstpart.Length, runs, size);
}
ReadBytes(4 * runs + size);
return encoding.GetString(bytes);
}
BearRui 2007-07-12
  • 打赏
  • 举报
回复
看提示应该是编码问题,楼主贴详细点代码看看。
不知道是否跟文件编码有关,也可以试试其他文件有无问题!!!
真相重于对错 2007-07-12
  • 打赏
  • 举报
回复
code??

110,534

社区成员

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

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

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