我通过创建打开文件对话框,打开某个文件,并讲文件名存到newFilePath变量中
用MessageBox显示newFilePath为:C:\Users\Administrator\Desktop\map.txt
然后调用CopyFile()函数复制文件到程序目录的子目录map文件下
CopyFile(newFilePath, _T("map\\map.txt"), FALSE);
但是复制不了,GetLastError()返回3,找不到路径
但是我换成这样又复制成功了:
CopyFile("C:/Users/Administrator/Desktop/map.txt", _T("map\\map.txt"), FALSE);
换成这样也能复制
CopyFile("C:\\Users\\Administrator\Desktop\\map.txt", _T("map\\map.txt"), FALSE);我想是不是因为newFilePath变量里的路径不对?
于是我写了一个转换的函数,将变量newFilePath里的路径转换成:C:\\Users\\Administrator\Desktop\\map.txt
然后再调用,结果还是失败,GetLastError()返回3于是我再将newFilePath转换成这个:C:/Users/Administrator/Desktop/map.txt
调用后结果还是失败难道变量是不能作为CopyFile()函数的第一个参数的?这个问题困扰了我一天
望高手能解决或者有一个更便捷的复制文件到程序所在目录的子目录的方法
用MessageBox显示newFilePath为:C:\Users\Administrator\Desktop\map.txt
然后调用CopyFile()函数复制文件到程序目录的子目录map文件下
CopyFile(newFilePath, _T("map\\map.txt"), FALSE);
但是复制不了,GetLastError()返回3,找不到路径
但是我换成这样又复制成功了:
CopyFile("C:/Users/Administrator/Desktop/map.txt", _T("map\\map.txt"), FALSE);
换成这样也能复制
CopyFile("C:\\Users\\Administrator\Desktop\\map.txt", _T("map\\map.txt"), FALSE);我想是不是因为newFilePath变量里的路径不对?
于是我写了一个转换的函数,将变量newFilePath里的路径转换成:C:\\Users\\Administrator\Desktop\\map.txt
然后再调用,结果还是失败,GetLastError()返回3于是我再将newFilePath转换成这个:C:/Users/Administrator/Desktop/map.txt
调用后结果还是失败难道变量是不能作为CopyFile()函数的第一个参数的?这个问题困扰了我一天
望高手能解决或者有一个更便捷的复制文件到程序所在目录的子目录的方法
// 需要设置CFileDialog不更改当前路径
CFileDialog dlg(TRUE, NULL, NULL, OFN_EXPLORER|OFN_NOCHANGEDIR);
{
// TODO: Add your control notification handler code here
char szBuffer[MAX_PATH * 10] = {0};
OPENFILENAME ofn = {0};
ofn.hwndOwner = m_hWnd;
ofn.lpstrFileTitle = "请选择地图";
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = _T("地图文件(*.w3x) \0 *.w3x");
ofn.nFilterIndex = 0;
ofn.lpstrFile = szBuffer;
ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
ofn.lpstrInitialDir = _T("C:\\");
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER; BOOL bSel = GetOpenFileName(&ofn);
SetDlgItemText(IDC_MAPSRC, szBuffer);
UpdateData();
}
复制文件部分代码: MPQHANDLE hFile = NULL;
CString src="";
char* file; //得到地图文件路径
GetDlgItem(IDC_MAPSRC)->GetWindowText(src); //转换文件路径
file = src.GetBuffer(src.GetLength());
CString newFilePath = filePath(file);
CopyFile(newFilePath, _T("map\\map.w3x"), FALSE);
if(FALSE==SFileOpenArchive("map\\map.w3x", 1, 0, &hFile))
{
MessageBox("请选择地图文件!","提醒");
return;
}
改变变量储存路径的代码CString CMyDlg::filePath(char* sourceFile)
{
CString newPath=sourceFile;
for(int i=0; i<strlen(sourceFile); i++)
{
if('\\'==sourceFile[i])
{
newPath.SetAt(i,'/');
}
}
return newPath;
}
CopyFile("C:\\Users\\Administrator\Desktop\\map.txt", _T("D:\\map\\map.txt"), FALSE);或者SetCurrentDirectory 设置当前路径
CFileDialog dlg(TRUE);
//保存当前路径
GetCurrentDirectory(MAX_PATH,szDirectory); int nRetn = dlg.DoModal();
if ( nRetn == IDOK )
{
SetCurrentDirectory(szDirectory);
//复制指定的文件
if (!CopyFile(dlg.GetPathName(),_T(".\\Debug\\OK"),FALSE) )
{
CString cs;
cs.Format(_T("错误码:%d"),GetLastError());
AfxMessageBox(cs);
}
else
AfxMessageBox(_T("复制成功")); }
我用的是VC 6.0 MFC开发的
如果用绝对路径
不可能叫我每次对不同的文件操作都要去改一下路径再编译.exe吧?
如果是这样那还不是手动复制文件到程序目录下方便
少了一个\
用C:\\Users\\Administrator\\Desktop\\map.txt根据你的错误,肯定是路径问题。
// 是CFileDialog默认会改变CurrentDirectroy
// 方法
// 1、在创建CFileDialog的时候,加上 OFN_NOCHANGEDIR 参数
// 2、在 CFileDialog::DoModal()之前,GetCurrentDirectory(), 关闭FileDialog之后,再SetCurrentDirectory
// 详见9楼