环境: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失败!
问题代码:(在按钮事件下)
OnBnClickedOpen()
{
。
FILE *fi,*afi;
if((fi=fopen(pathName,"rb+"))==NULL)
{
MessageBox("打开1失败!");
return;
} if(( afi=fopen("a.bmp","rb"))==NULL)
{
MessageBox("打开2失败!");
return;
}
。。
}
总会弹出 打开2失败!
解决方案 »
- '*/' found outside of comment 这个警告是怎么回事
- VC listcontrol的重绘问题(高手乱入)
- 杜比原理的声卡如何操作?(选择声道,录音等)
- 救命啊,关于超线程的诡异问题!
- 网络通信vc编程怎样入手?
- 请问工具栏上的文本框怎样取得它的内容呀?
- Ado 扩展中对字段是怎么绑定的
- 想在dialogbox里加好几张图片,并要会换图片 怎么搞呢 。有详细代码吗
- 初来咋到,有个问题请教,怎么转移分,我还不知道
- 如何显示CRecordView?(就象CDialog可以用DoModal)
- VC6 SDI框架菜单向下三行,第一行放置两行宽的图标那样图片?
- CFile类读写文件的问题
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;
}
if(fileDlg.DoModal()==IDOK)
pathName = fileDlg.GetPathName();
else
return;
{
FILE *fi,*afi;
if((fi=fopen("1.txt","rb+"))==NULL)
{
AfxMessageBox("打开1失败!");
return;
} if(( afi=fopen("1.txt","rb"))==NULL)
{
AfxMessageBox("打开2失败!");
return;
}
}
我这样弄的
这样行的,我下面贴出的就不行了!
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失败!");
}
}
确实出现了你说的问题
应该是CFileDialog改变了相对路径
看看这个
http://www.cnblogs.com/waterlin/archive/2009/07/02/1670397.html
看看是什么 错误
用绝对路径就对了,哎!
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");}
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.用绝对路径打开过文件以后,当前工作路径目录就变成那个文件所在目录了。