环境:VS2005 建立的是MFC(去掉了UNICODE选项)
问题代码:(在按钮事件下)
OnBnClickedOpen()
{

  FILE *fi,*afi;
  if((fi=fopen(pathName,"rb+"))==NULL)
{
MessageBox("打开1失败!");
return;
} if(( afi=fopen("a.bmp","rb"))==NULL)
{
MessageBox("打开2失败!");
return;
}
。。
}
总会弹出  打开2失败!

解决方案 »

  1.   

    if(( afi=fopen("a.bmp","rb"))==NULL) //换成完整路径试试。
      

  2.   

    我是在当前文件夹下放了 A.BMP,不行的。好像在一个事件函数中只能有一个FOPEN函数,我把另一个FOPEN放在OnInitDialog就好了
      

  3.   

    晕 正常执行。。
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
    FILE *fi,*afi;
     if((fi=fopen("1.txt","rb+"))==NULL)
     {
     AfxMessageBox("打开1失败!");
     return 0;
     }  if(( afi=fopen("1.txt","rb"))==NULL)
     {
     AfxMessageBox("打开2失败!");
     return 0; 
     }  return 0;
    }
      

  4.   

    CFileDialog fileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING, 0,NULL);
    if(fileDlg.DoModal()==IDOK)
    pathName = fileDlg.GetPathName();
    else 
    return;
      

  5.   

    void CCustomCtrlDlg::OnBnClickedButton1()
    {
    FILE *fi,*afi;
    if((fi=fopen("1.txt","rb+"))==NULL)
    {
    AfxMessageBox("打开1失败!");
    return;
    } if(( afi=fopen("1.txt","rb"))==NULL)
    {
    AfxMessageBox("打开2失败!");
    return;  
    }
    }
    我这样弄的
      

  6.   


    这样行的,我下面贴出的就不行了!
    void CMytDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    CString pathName;
    FILE *fi,*fii;
    CFileDialog fileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING, 0,NULL);
    if(fileDlg.DoModal()==IDOK)
    pathName = fileDlg.GetPathName();
    else 
    return; if((fi=fopen(pathName,"rb+"))==NULL)
    {
    MessageBox("打开1失败!");
    return;
    } if(( fii=fopen("water.bmp","rb+"))==NULL)
    {
    MessageBox("打开2失败!");
    }
    }
      

  7.   

    我又试了一下
    确实出现了你说的问题
    应该是CFileDialog改变了相对路径
    看看这个
    http://www.cnblogs.com/waterlin/archive/2009/07/02/1670397.html
      

  8.   

    GetLastError
    看看是什么 错误
      

  9.   


    用绝对路径就对了,哎!
    void CprotxtDlg::OnBnClickedButton1()
    {
    // TODO: 在此添加控件通知处理程序代码
    FILE *fi,*fii;
    char buffer[10];
    BITMAPFILEHEADER bmfh;
    BITMAPINFO bmi;
    BITMAPFILEHEADER ibmfh;
    BITMAPINFO ibmi;
    CString pathname;

    CFileDialog fileDlg (TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |OFN_ALLOWMULTISELECT|OFN_ENABLESIZING, 0,NULL);
    if(fileDlg.DoModal()==IDOK)
    pathname = fileDlg.GetPathName();
    else 
    return;
    if((fi=fopen(pathname,"rb"))==NULL)
    MessageBox("wrong1");

    if((fii=fopen("G:\\Visual Studio 2005\\Projects\\protxt\\protxt\\water.bmp","rb"))==NULL)
    MessageBox("wrong2");}
      

  10.   

    我也碰到了你的情况,VC 6下用C的fopen,相对路径读取时就失败,而新建时却正确,但是绝对路径下就正常(以前这样做都成功,现在不知咋的了)。无奈,我就写了一个相对转绝对的转换函数:    //设置和获取当前工作路径
        char strCurPath[MAX_PATH] = "";
        GetModuleFileName(NULL, strCurPath, MAX_PATH);
        (strrchr(strCurPath, '\\'))[1] = 0;  //以\结尾
        strcpy(m_aszCurPath, strCurPath);FILE* FOPEN(const char * const pszFileName, const char * const pszFlag)
    {
        char aszAbsolutePath[MAX_PATH] = "";
        if (NULL == strchr(pszFileName, '\\')) //若pszFileName是相对路径
        {
            strcpy(aszAbsolutePath, m_aszCurPath);
            strcat(aszAbsolutePath, pszFileName);
        }
        else
        {
            strcpy(aszAbsolutePath, pszFileName);
        }
        
        return fopen(aszAbsolutePath, pszFlag);
    }很容易解决,打开文件一定要用绝对路径。 
    用绝对路径吧,相对路径,有可能有时候会找得到,有时候找不到
    用绝对路径,相对路径不稳定 
    全部使用绝对路径,或者使用SetCurrentDirectory设置当前目录后再使用相对路径
    1.相对路径,当你调用过CFileDialog后,它的默认的当前工作目录就变了,还是绝对路径比较放心~  
    2.用绝对路径打开过文件以后,当前工作路径目录就变成那个文件所在目录了。