【问题标题】:What is the correct input format for windows CopyFile?windows CopyFile 的正确输入格式是什么?
【发布时间】:2012-07-19 22:43:00
【问题描述】:

我正在尝试使用 Windows CopyFile 功能复制一个文件并将其重命名为另一个文件夹中的另一个文件。但是,即使我给它的路径是正确的并且文件和文件夹都存在,它总是返回路径有问题。我究竟做错了什么?

使用“C:\Dummy.png”作为源,使用“C:\Dest”作为目标。

void CreateDummyItemsAssetsPNG()
{
    string  DummyAsset;  
    string  dummyDestination; 

    cout<<"Please Provide dummy file asset that is a .png: "; 
    cin>>DummyAsset; 

    cout<<"Please Provide a Destination: "; 
    cin>>dummyDestination; 

    vector<string>::iterator itor; 
    string fullDest; 

    for(itor = listOfItems.begin(); itor<listOfItems.end(); ++itor)
    {
        fullDest.clear(); 
        fullDest = dummyDestination + "\\"+ (*itor)+".png"; 
        cout<<"Copy: "<<DummyAsset<<" TO: "<<fullDest<<endl; 
        if(!CopyFile(LPCTSTR(DummyAsset.c_str()),LPCTSTR(dummyDestination.c_str()),false) )
        {
            printf("Could not copy file.\n"); 
            cout<<GetLastError()<<endl; 
        }
    }
}

谢谢!

【问题讨论】:

  • c_str() 转换为LPTSTR doesn't actually make it TSTR
  • 扩展 GSerg 的评论:由于您使用的是 8 位字符串,因此请使用 CopyFileA 而不是 CopyFile。

标签: c++ windows winapi


【解决方案1】:

CopyFile() 需要一个文件 name 作为第二个参数,而您只传递目标 directory。指定全名(您似乎在 fullDest 中执行此操作),这应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多