CFile 读hex文件发生错误, 访问xxx时发生未知错误

女神打Boss 2017-05-26 02:56:38
我是对话框程序,添加按钮点击事件响应函数
代码

CString fileName;
char* strText = "";
CString data;
fileName = "D:\\HEX\\a1";
//"C:\\Users\\Administrator\\Desktop\\a1"


CFile openfile(fileName, CFile::modeRead);
int i = 0;
openfile.SeekToBegin();

while(openfile.Read(strText, 40) != 0)//调试在这里出问题
{
data.Format("%s", strText);
AfxMessageBox(data);
i++;
openfile.Seek(40*i, CFile::begin);
}
openfile.Close();

在Read时跳转到wincore.cpp文件的LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
WPARAM wParam = 0, LPARAM lParam = 0) 这个函数中lResult = AfxProcessWndProcException(e, &pThreadState->m_lastSentMsg);这句后弹出对话框如图




...全文
985 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
三岁、就很帅 2017-05-26
  • 打赏
  • 举报
回复
    char* pszFileName = "C://a1.txt";
    CStdioFile myFile;
    CFileException fileException;
    CString str1  = _T("");
    CString temp;
    if (myFile.Open(pszFileName, CFile::typeText | CFile::modeReadWrite), &fileException)
    {
        myFile.SeekToBegin();   
        while (myFile.ReadString(temp)) {
            str1 += temp;
        }   
    }
    else
    {
        TRACE("Can't open file %s,error=%u/n", pszFileName, fileException.m_cause);
    }
    MessageBox(str1);
    myFile.Close();
worldy 2017-05-26
  • 打赏
  • 举报
回复
char* strText = "";没有分配空间
zgl7903 2017-05-26
  • 打赏
  • 举报
回复
try
{
  CFile openfile(fileName, CFile::modeRead);
  
  while(1)
  {
     char strText [41] = {0};
     if(openfile.Read(strText, 40))
     {
       CString data(strText);   
       AfxMessageBox(data);
     }
     else
     {
        break;
     }
  }
  openfile.Close();
}
catch(CFileException *e)
{
  e->ReportError();
  e->Delete();
}
三岁、就很帅 2017-05-26
  • 打赏
  • 举报
回复
char* pszFileName="C://myfile.txt"; CStdioFile myFile; CFileException fileException; if(myFile.Open(pszFileName,CFile::typeText|CFile::modeReadWrite),&fileException) { myFile.SeekToBegin(); CString str1; myFile.ReadString(str1); CString str2; myFile.ReadString(str2); AfxMessageBox(str1+str2); } else { TRACE("Can't open file %s,error=%u/n",pszFileName,fileException.m_cause); } myFile.Close();
schlafenhamster 2017-05-26
  • 打赏
  • 举报
回复
CFile openfile(fileName, CFile::modeRead); int len = openfile.GetLength(); strText = new char[len+1]; . . delete strText;
三岁、就很帅 2017-05-26
  • 打赏
  • 举报
回复
CString也有这个问题 就定义个固定大小的指针数组吧 我以前也碰到过 最近学QT MFC有点生疏了块
三岁、就很帅 2017-05-26
  • 打赏
  • 举报
回复
女神打Boss 2017-05-26
  • 打赏
  • 举报
回复
引用 1 楼 swwllx 的回复:
char* strText = "";的长度没有分配 你先分配一个指定大小的空间就没啥了 或者干脆用CString吧
正解 用CString 怎么用
三岁、就很帅 2017-05-26
  • 打赏
  • 举报
回复
char* strText = "";的长度没有分配 你先分配一个指定大小的空间就没啥了 或者干脆用CString吧

2,586

社区成员

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

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