求助:CopyFile的用法

whatwhyhope 2009-06-03 04:25:56
各位高手帮帮忙!
const TCHAR* pFile1 = _T("D:\\Src\\foo1.txt");
const TCHAR* pFile2 = _T("C:\\Documents and Settings\\foo1.txt");

CopyFile(_T("D:\\Src\\foo1.txt"),_T("C:\\Documents and Settings\\foo1.txt"),DK_FALSE);// 直接将字符串写入参数,这样可以拷贝文件。

CopyFile(pFile1, pFile2, DK_FALSE); // 这样不能拷贝文件,错在什么地方了?
...全文
104 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
whatwhyhope 2009-06-03
  • 打赏
  • 举报
回复
呵呵,找到原因了,太马虎了,找错目录了。多谢大家的参与,谢谢...
「已注销」 2009-06-03
  • 打赏
  • 举报
回复
Unicode版本也没问题。
#define UNICODE
#define _UNICODE

#include <iostream>
#include <tchar.h>
#include <windows.h>

using namespace std;

const TCHAR* pFile1 = _T("D:\\Src\\foo1.txt");
const TCHAR* pFile2 = _T("C:\\Documents and Settings\\foo1.txt");

int main()
{
CopyFile(pFile1, pFile2, FALSE);
return 0;
}
waterathena 2009-06-03
  • 打赏
  • 举报
回复
我在VS2005中验证过代码:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{

const TCHAR pFile1[MAX_PATH] = _T("D:\\1.txt");
const TCHAR pFile2[MAX_PATH] = _T("C:\\1.txt");
CopyFile(pFile1, pFile2, false);

getchar();
return 0;
}

可以拷贝文件。即使C盘不存在1.txt文件。
「已注销」 2009-06-03
  • 打赏
  • 举报
回复
#include <iostream>
#include <tchar.h>
#include <windows.h>

using namespace std;

const TCHAR* pFile1 = _T("D:\\Src\\foo1.txt");
const TCHAR* pFile2 = _T("C:\\Documents and Settings\\foo1.txt");

int main()
{
CopyFile(pFile1, pFile2, FALSE);
return 0;
}


测试通过。
「已注销」 2009-06-03
  • 打赏
  • 举报
回复
为什么拷贝不了?
什么错误?
Treazy 2009-06-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 whatwhyhope 的帖子:]
各位高手帮帮忙!
const TCHAR* pFile1 = _T("D:\\Src\\foo1.txt");
const TCHAR* pFile2 = _T("C:\\Documents and Settings\\foo1.txt");

CopyFile(_T("D:\\Src\\foo1.txt"),_T("C:\\Documents and Settings\\foo1.txt"),DK_FALSE);// 直接将字符串写入参数,这样可以拷贝文件。

CopyFile(pFile1, pFile2, DK_FALSE); // 这样不能拷贝文件,错在什么地方了?
[/Quote]

楼主,代码应该是没问题的
你对CopyFile的返回值判断一下
如果为0,查看一下error code看看是多少
类似于下面的代码

if(0 == CopyFile(pFile1, pFile2, FALSE))
{
printf("Error: %d", GetLastError());
}
waterathena 2009-06-03
  • 打赏
  • 举报
回复
const TCHAR pFile1[MAX_PATH} = _T("D:\\Src\\foo1.txt");
const TCHAR pFile2[MAX_PATH] = _T("C:\\Documents and Settings\\foo1.txt");

CopyFile(pFile1, pFile2, DK_FALSE);

不过在我这里的是拷贝文件到相应目录下。且我的路径名字都是CString类型。
whatwhyhope 2009-06-03
  • 打赏
  • 举报
回复
一楼的老大,CopyFile(*pFile1, *pFile2, DK_FALSE);//这种写法会报错,cannot convert parameter 1 from 'const TCHAR' to 'LPCWSTR'
ztenv 版主 2009-06-03
  • 打赏
  • 举报
回复
CopyFile(*pFile1, *pFile2, DK_FALSE);

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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