帮忙看看这个代码什么问题

女神打Boss 2017-06-03 09:31:51
CString fileName = getModulePath() + "\\123.hex";
char strfile[64];
//strfile = new char[64];//存储从文件读取的数据
char strhex[128];

CFile hexfile(fileName, CFile::modeRead);//构造CFile对象
int i = 0;
hexfile.SeekToBegin();
int a = 0;
while(hexfile.Read(strfile, 64) != 0)//读出的文件不为空
{
int len = strlen(strfile); //len为什么等于75
for (a = 0; a < len; a++)
{
sprintf(strhex + 2*a, "%02X", (unsigned char)strfile[a]);
}

strFileArr.Add(strhex);
i++;
hexfile.Seek(64 * i, CFile::begin);//定位文件指针
}
//delete strfile;
hexfile.Close();//关闭文件

调试结果如图



问题 int len = strlen(strfile); //len为什么等于75

...全文
424 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
女神打Boss 2017-07-14
  • 打赏
  • 举报
回复
对的,确实没有结束符
赵4老师 2017-07-12
  • 打赏
  • 举报
回复
其实电脑开机后物理内存的每个字节中都有值且都是可读写的,从来不会因为所谓的new、delete或malloc、free而被创建、销毁。区别仅在于操作系统内存管理模块在你读写时是否能发现并是否采取相应动作而已。操作系统管理内存的粒度不是字节而是页,一页通常为4KB。
zhujinqiang 2017-07-11
  • 打赏
  • 举报
回复
引用 4 楼 worldy 的回复:
char strfile[64] //会在内存开辟一个64字节的空间,但是填充的是随机数 int len = strlen(strfile);//前面读到的数据没有值为0的字节,因此strlen会在数组之后继续找值为0的单元,直到找到,你本次的运行环境中在第76单元找到0,因此,len值为75 你下一次运行可能就不是75,可能是别的数,是个随机数了,也可能出现最坏的情况,查找超出可访问的内存范围,程序崩溃
正解!
羽毛乱发 2017-07-11
  • 打赏
  • 举报
回复
引用 5 楼 SilenceNet 的回复:
char strfile[64]; 你只定义了64个字节的空间 而你读数据把64个字节填满了, 而stlen 是以 字符0 为结束来计算字符串长度,这个0已经在64字节之外了 到这里内存已经有问题了 如果要使用stelen,那么char数组必须多留1个字节的位置,并且要置0 char charfile[65]={0}
正确。 楼主的做法有问题,应该先getlength,然后new,最后delete.
starytx 2017-06-05
  • 打赏
  • 举报
回复
没有正确的结束符呗
schlafenhamster 2017-06-03
  • 打赏
  • 举报
回复
i++; hexfile.Seek(64 * i, CFile::begin);//定位文件指针 都不要·
schlafenhamster 2017-06-03
  • 打赏
  • 举报
回复
CString fileName = getModulePath() + "\\123.hex"; char strfile[64]; //strfile = new char[64];//存储从文件读取的数据 char strhex[128]; CFile hexfile(fileName, CFile::modeRead);//构造CFile对象 int i = 0; hexfile.SeekToBegin(); int a = 0; int len; while(len=hexfile.Read(strfile, 64) != 0)//读出的文件不为空 { for (a = 0; a < len; a++) { sprintf(strhex + 2*a, "%02X", (unsigned char)strfile[a]); } strFileArr.Add(strhex); i++; hexfile.Seek(64 * i, CFile::begin);//定位文件指针 } //delete strfile;
hurryboylqs 2017-06-03
  • 打赏
  • 举报
回复
因为你没初始化strfile数组,要先清零,字符串计算长度是以0来结尾的 建议你不要用strlen来决定strfile的长度,而是ReadFile的返回值来决定
schlafenhamster 2017-06-03
  • 打赏
  • 举报
回复
参考:

//Read Binary File into Buffer
BOOL CShowHexDoc::ReadBin(CFile *pBinFile)
{
	m_nSize =pBinFile->GetLength();
	if(m_nSize==0)
	{
		AfxMessageBox("Binary file has 0 size !",MB_ICONSTOP);
		return FALSE;
	}
//
	if(m_pBuffer) delete []m_pBuffer;
//
	m_pBuffer=new BYTE[m_nSize];
	if(m_pBuffer==0)
	{
		AfxMessageBox("Not enough memory !",MB_ICONSTOP);
		return FALSE;	
	}
	pBinFile->ReadHuge(m_pBuffer,m_nSize); // 
//
	return TRUE;
}

BOOL CShowHexDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
// do not call base 
//	if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE;
	
// TODO: Add your specialized creation code here
	m_CurrentFileName=lpszPathName;
	CFile binfile;
	binfile.Open(lpszPathName,CFile::modeRead|CFile::shareExclusive);
	ReadBin(&binfile);
	binfile.Close();
// show 
	StringBin();
//
	return TRUE;
}
// Fill a string array in hex format
BOOL CShowHexDoc::StringBin()
{
	BYTE *pTmp=m_pBuffer;
	CString tmp;
	CString prompt;
	
	BYTE comment[40];
// if is there an old
	m_HexStrArray.RemoveAll();
//
	int FullRows=m_nSize/16;
	int PartRow=m_nSize%16;
//
	for(int rr=0;rr < FullRows ; rr++)
	{//
		prompt.Empty();
		tmp.Format("%06X:", 16*rr);//"1000000:"  16 bytes
		prompt+=tmp;
		for(int col = 0;col < 16; col++)
		{
			if((*pTmp>' ') && (*pTmp<'z')) comment[col]=*pTmp;
			else                           comment[col]='.';
			tmp.Format("%02X ", *pTmp++);
			prompt+=tmp;
		}
		comment[col]=0;
		prompt+="    ";
		prompt+=comment;
		prompt+="\r\n";
		m_HexStrArray.Add(prompt);
	}
//	afxDump << prompt ;// 0x383FFF
	if(PartRow!=0)
	{
		prompt.Empty();
		tmp.Format("%06X:", 16*rr);
		prompt+=tmp;
		for(int col = 0;col < PartRow; col++)
		{
			if((*pTmp>' ') && (*pTmp<'z')) comment[col]=*pTmp;
			else                           comment[col]='.';
			tmp.Format("%02X ", *pTmp++);
			prompt+=tmp;
		}
		comment[col]=0;
		while(col<16)
		{// more spaces
			prompt+="   ";//3
			col++;
		}
		prompt+="    ";
		prompt+=comment;
		prompt+="\r\n";
		m_HexStrArray.Add(prompt);
	}
//
	return TRUE;
}
schlafenhamster 2017-06-03
  • 打赏
  • 举报
回复
char strfile[64]; 其实应该改名为 BYTE HexValue[64]; len=hexfile.Read(strfile, 64) 是 每次 读 64 个 Hex ,可能 < 64 最后一次。 读完后 文件指针 自动 后移 64 个 字节。
silencenet 2017-06-03
  • 打赏
  • 举报
回复
char strfile[64]; 你只定义了64个字节的空间 而你读数据把64个字节填满了, 而stlen 是以 字符0 为结束来计算字符串长度,这个0已经在64字节之外了 到这里内存已经有问题了 如果要使用stelen,那么char数组必须多留1个字节的位置,并且要置0 char charfile[65]={0}
worldy 2017-06-03
  • 打赏
  • 举报
回复
char strfile[64] //会在内存开辟一个64字节的空间,但是填充的是随机数 int len = strlen(strfile);//前面读到的数据没有值为0的字节,因此strlen会在数组之后继续找值为0的单元,直到找到,你本次的运行环境中在第76单元找到0,因此,len值为75 你下一次运行可能就不是75,可能是别的数,是个随机数了,也可能出现最坏的情况,查找超出可访问的内存范围,程序崩溃

2,586

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 资源
社区管理员
  • 资源
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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