免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1775 | 回复: 0
打印 上一主题 下一主题

[函数] 求助:API函数CopyFile()不能复制文件问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-27 02:52 |只看该作者 |倒序浏览
#include <iostream>;
#include <windows.h>;
#include <cstring>;
using namespace std;

const int MAXFILE = 500;

//SROUCE FILE STRUCT
typedef struct _SRCFILE
{
        WIN32_FIND_DATA fd;
        bool bIsNew;
       
}SRCFILE;





int main(int argc, char* argv[])
{
       
        if (argv[1] == 0)
        {
                return (-1);
        }
       
        WIN32_FIND_DATA fd;
       
        SRCFILE srcFile[MAXFILE];
        bool isfile = true;
        int isSrcFile = 0;
        HANDLE hFind = INVALID_HANDLE_VALUE;
        char DirSpec[100];
        strncpy (DirSpec, argv[1], strlen(argv[1])+1);
                strncat (DirSpec, "\\*", 3);
                char DestDir[100];
        strncpy (DestDir, argv[1], strlen(argv[1])+1);
        *DestDir = 'E';
        CreateDirectory(DestDir, NULL) ;
       

        //deel with the source directory
        hFind = FindFirstFile(DirSpec,&fd);
        if (hFind == INVALID_HANDLE_VALUE)
        {
                cout<<"No files!"<<endl;
                return (-1);
        }
       
        else
        {
               
                while (hFind != INVALID_HANDLE_VALUE && isfile)
                {
                        if (fd.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE)
                        {
                                srcFile[isSrcFile].fd = fd;
                                srcFile[isSrcFile].bIsNew = FALSE;
                                isSrcFile++;
                                cout<<fd.cFileName<<endl;
                        }
                        isfile = FindNextFile(hFind, &fd);
                       
                       
               
                }
        }
        //cout<<srcFile[0].fd.cFileName<<":"<<DestDir<<
                endl;
        //strncat (DestDir, "\\", 2);
        if (CopyFile(srcFile[0].fd.cFileName,DestDir,FALSE))
        {
                cout<<"copy action is ok!"<<endl;
               
        }
        else
        {
                printf("Could not copy file.\n";
        return 0;

        }

测试的结果总是:Could not copy file.看了msdn中该函数的定义及参数要求,只有当目标文件夹是"只读"属性时才会失败,我的系统是xp,好象xp下文件夹属性都是默认只读的,改过后还会还原到只读.不知道是不是这原因.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP