[C++] 复制文件 →→→→→进入此内容的聊天室

来自 , 2020-03-28, 写在 C++, 查看 101 次.
URL http://www.code666.cn/view/5a7f963e
  1. #include<iostream>
  2. #include<fstream>
  3. #include<io.h>
  4. #include<windows.h>
  5. using namespace std ;
  6.  
  7. /**复制文件
  8. filename :要复制的文件名
  9. newfile :要复制到的文件名
  10. */
  11. int cpy(char*filename,char*newfile)
  12. {
  13.    ifstream in ;
  14.    ofstream out ;
  15.    /**
  16.    open函数的原型是
  17.    open(const char*filename,ios_base::openmode mode=ios_base::in) ;
  18.    in_stream.open("file.txt");这样写参数实际上是指向这个常量字符串的指针
  19.    */
  20.    //打开文件
  21.    in.open(filename);
  22.    //打开文件失败
  23.   if(in.fail()){
  24.     cout<<"打开文件失败"<<endl ;
  25.      in.close();
  26.      out.close();
  27.      return 0 ;
  28.    }
  29.   out.open(newfile);
  30.   if(out.fail()){
  31.     cout<<"创建文件失败"<<endl ;
  32.      in.close();
  33.      out.close();
  34.      return 0 ;
  35.   }else{//复制文件
  36.    out<<in.rdbuf();
  37.    out.close();
  38.    in.close();
  39.    return 1 ;
  40.   }
  41. }

回复 "复制文件"

这儿你可以回复上面这条便签

captcha