编的一个程序COPY文件的,总是出错,不知道是不是输入的格式问题?
#include "stdafx.h"
#include <windows.h>
#include <iostream>using namespace std;
#pragma comment(lib,"msvcrtd.lib")
int _tmain(int argc, _TCHAR* argv[])
{
char SourceFileName[MAX_PATH];
char DestFileName[MAX_PATH];
BOOL Success;
cout << "please input your source file name"<< endl;
cin >> SourceFileName;
   cout << SourceFileName<<endl;
cout << "please input your dest file name" << endl;
cin >> DestFileName;
     LPCWSTR oldname;
 LPCWSTR newname;
 oldname = (LPCWSTR)SourceFileName;
 newname = (LPCWSTR)DestFileName; Success = CopyFile(oldname,newname,TRUE); if(!Success) cout << "fail to open that file.the error code is "<< GetLastError<< endl; else 
cout << "the file copyed is success" << endl; system("PAUSE"); 
return 0;
}
打开文件总是失败,文件在目录中是存在的

解决方案 »

  1.   

    BOOL CopyFile(
      LPCTSTR lpExistingFileName, // name of an existing file
      LPCTSTR lpNewFileName,      // name of new file
      BOOL bFailIfExists          // operation if file exists
    );没说参数是wide char啊
      

  2.   

    程序可以运行的,只是我输入文件地址的时候,总是执行这句
    cout << "fail to open that file.the error code is "<< GetLastError<< 
    我输入时的复制地址D:\\j\\j.exe和D:\\j\\j.exe都不行,程序执行不了复制。
      

  3.   

    为什么要转化成宽字符oldname = (LPCWSTR)SourceFileName;?
    直接
    Success = CopyFile(SourceFileName,DestFileName,TRUE);
    不行吗?
      

  4.   

    不行呀,不加就会出这个。
    ocuments and settings\gq\my documents\visual studio 2005\projects\consel\consel\consel.cpp(21) : error C2664: 'CopyFileW' : cannot convert parameter 1 from 'char [260]' to 'LPCWSTR'
      

  5.   

    是這樣的
    你沒有必要使用LPCWSTR,把所有的LPCWSTR換成LPCTSTR就可以了
    地址輸入的時候用D:\j\j.exe,不用雙斜綫。
    我不清楚你用的編譯器是什麽,在vs2003中LPCWSTR根本就無法轉換為LPCTSTR
      

  6.   

    既然已经getlasterror,那就在error lookup里面查查是什么原因
      

  7.   

    还是不行呀,郁闷这小点程序都不出来了,我用的是VS200
    错误代码是7C930331应该是文件打不开吧
      

  8.   

    还是不行呀,郁闷这小点程序都不出来了,我用的是VS2005  
    错误代码是7C930331应该是文件打不开吧
      

  9.   

    项目编码??
    VS2005 默认 unicode 改一下
      

  10.   

    hdt(倦怠) 和alfwolf(木马煞)加在一起就可以了谢谢大家,在项目->属性配置->常规->字符集改成多字节,就行了,为什么要这样改呀,VS默认的unicode什么时候用.