您的位置:程序门 -> c/c++ -> c++ 语言



文件复制问题,急!~~~~~~~~在先等


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


文件复制问题,急!~~~~~~~~在先等[已结贴,结贴人:xiaosu1983]
发表于:2008-01-21 15:55:29 楼主
(本程序可以在vc++6.0顺利通过)
#include <iostream>
#include <string>
#include   "windows.h"
#include   "tchar.h"
using   namespace   std;
void   main()
{
  /*
  文件a.txt从d盘复制到e盘,并且命名为b.txt,若e盘中有文件b.txt,则覆盖之
  */  
  tchar   s1[]=   "d:\\a.txt";
  tchar   s2[]=   "e:\\b.txt";
  copyfile(s1,s2,false);
}
现在有个问题就是:我要把"d:\\a.txt"和"e:\\b.txt"分别作为输入,此时程序出现问题了
程序如下(省略头文件)
void   main()
{
    string   source,destination;
    cin> > source> > destination;
    tchar   s1[]   =   source;
    tchar   s2[]   =   destination;
    copyfile(s1,s2,false);
}
编译时出错,意思就是说不能转化,这个问题我不会处理,请各位大虾帮忙看一下,帮我解决一下,谢谢!
发表于:2008-01-21 16:04:331楼 得分:0
出错信息粘上来
发表于:2008-01-21 16:10:002楼 得分:0
c/c++ code
void main() { string source,destination; cin> > source> > destination; tchar s1[] = source.c_str(); //std::string 对象转化成char *,要用他的c_str方法。 tchar s2[] = destination.c_str(); // copyfile(s1,s2,false); }


你试试吧。
发表于:2008-01-21 16:10:453楼 得分:0
d:\microsoft   visual   studio\myprojects\int\int1.cpp(13)   :   error   c2440:   'initializing'   :   cannot   convert   from   'class   std::basic_string <char,struct   std::char_traits <char> ,class   std::allocator <char>   > '   to   'char   []'
                no   user-defined-conversion   operator   available   that   can   perform   this   conversion,   or   the   operator   cannot   be   called
d:\microsoft   visual   studio\myprojects\int\int1.cpp(14)   :   error   c2440:   'initializing'   :   cannot   convert   from   'class   std::basic_string <char,struct   std::char_traits <char> ,class   std::allocator <char>   > '   to   'char   []'
                no   user-defined-conversion   operator   available   that   can   perform   this   conversion,   or   the   operator   cannot   be   called
error   EXECuting   cl.exe.
发表于:2008-01-21 16:12:534楼 得分:0
回复3楼的,还是不行!错误信息如下
d:\microsoft   visual   studio\myprojects\int\int1.cpp(13)   :   error   c2440:   'initializing'   :   cannot   convert   from   'const   char   *'   to   'char   []'
                there   are   no   conversions   to   array   types,   although   there   are   conversions   to   references   or   pointers   to   arrays
d:\microsoft   visual   studio\myprojects\int\int1.cpp(14)   :   error   c2440:   'initializing'   :   cannot   convert   from   'const   char   *'   to   'char   []'
                there   are   no   conversions   to   array   types,   although   there   are   conversions   to   references   or   pointers   to   arrays
error   EXECuting   cl.exe.
发表于:2008-01-21 16:15:205楼 得分:0
void       main()
{
        string       source,destination;
        cin>   >   source>   >   destination;
        copyfile(source,destination,false);
}  
发表于:2008-01-21 16:19:286楼 得分:0
楼上编的程序我之前已经编过了,还是不行!
msdn里讲的就是copyfile(string^   source,string^   destination,boolean   bool)
为什么用的时候就不可以呢!
发表于:2008-01-21 16:20:047楼 得分:10
void               main()
{
                tchar               source[256],destination[256];
                cin>       >       source>       >       destination;
                copyfile(source,destination,false);
}    
发表于:2008-01-21 16:22:018楼 得分:10
c/c++ code
void main() { string source,destination; cin>> source>> destination; //tchar s1[] = source.c_str(); tchar s1[20]; strcpy(s1, source.c_str()); //tchar s2[] = destination.c_str(); tchar s2[20]; strcpy(s2, destination.c_str()); copyfile(s1,s2,false); }

这个可以了。我跟踪了一下。s1,s2变量的值是对的。
发表于:2008-01-21 16:28:549楼 得分:0
类型转换问题。。
发表于:2008-01-21 16:35:3610楼 得分:0
非常感谢噢!
发表于:2008-01-21 16:43:1911楼 得分:0
给分,给分,分最现实。嘿嘿。
发表于:2008-01-21 16:44:0112楼 得分:0
帖子给分怎么给啊,以前给过的,现在好像换了啊!
请告知!
发表于:2008-01-21 16:46:3413楼 得分:0
嘿嘿,给过了,由于是两个人都给出了正确答案,所以只能一人10分啊!
哈哈!


快速检索

最新资讯
热门点击