首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

CopyFile函数的有关问题

2012-02-11 
CopyFile函数的问题 编的一个程序COPY文件的,总是出错,不知道是不是输入的格式问题?#includestdafx.h #i

CopyFile函数的问题

编的一个程序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;
}


打开文件总是失败,文件在目录中是存在的


[解决办法]

int main(int argc,char* argv[])
{
string src,des;
BOOL Success;
cout < < "please input your source file name " < < endl;
cin > > src;
cout < < src < <endl;
cout < < "please input your dest file name " < < endl;
cin > > des;
Success = CopyFileA(src.c_str() ,des.c_str(),TRUE);
if(!Success)
cout < < "fail to open that file.the error code is " < < GetLastError < < endl;
else
cout < < "the file copyed is success " < < endl;

return 0;
}

热点排行