|
发表于 2013-5-10 16:01:01
|
显示全部楼层
命令不术一样, 不过打开后用函数实现就行了吧。我写的就是这样的:- /*2013年 04月 27日 星期六 20:08:09 HKT
- 2 *用c++实现文件复制功能函数的编写
- 3 *要读入与写入函数,ifstream, ofstream
- 4 */
- 5 #include <fstream> //包含文件的读入与写入函数
- 6 #include <iostream>
- 7 using namespace std;
- 8 int main(int argc, char *argv[])
- 9 {
- 10 //首先, 查看输入的格式对不对
- 11 if(argc != 3)
- 12 {
- 13 cerr << "格式为: copy 源文件 目标文件" << endl;
- 14 return 0; //退出程序
- 15 }
- 16 //当格式正解时
- 17 ifstream input(argv[1]); //把源文件的内存地址赋给input,默认以in打开
- 18 if(!input)
- 19 {
- 20 cerr << "打开文件失败!" << endl;
- 21 return -1;
- 22 }
- 23 ofstream output(argv[2]); //把目标文件的指针赋给output, 默认以out打开
- 24 if(!output)
- 25 {
- 26 cerr << "打开文件失败!" << endl;
- 27 input.close(); //关闭文件指针
- 28 return -1;
- 29 }
- 30 //如果打开成功, 那就准备复制
- 31 output << input.rdbuf(); //直接从缓冲区复制到另一文件
- 32 cout << "成功复制" << endl;
- 33 //关闭
- 34 input.close();
- 35 output.close();
- 36 return 0;
- 37 }
复制代码 |
|