|
发表于 2012-3-2 22:11:49
|
显示全部楼层
一句话是用来命令行参数,来复制文件
详细注视:- 01.#include <fstream> //C++文件流类头文件
- 02.#include <cstdlib>
-
- 03.#include <iostream>
-
- 04.using namespace std;
-
- 05.void print_error( const char*, const char* = "" );
-
- 06.int main( int argc, char* argv[] )
- 07.{
- 08. if( argc != 3 ) //Main函数的第一个参数是参数的个数,如果不是三个就错误
- 09. {
- 10. print_error( "输入形式: copyFile 源文件名 目标文件名 \n" );
- 11. }
-
- 12. ifstream in( argv[1], ios::binary ); //argv是一个参数数组,第二个就是源文件
- 13. if( !in )
- 14. {
- 15. print_error( "不能打开文件: ", argv[1] );
- 16. }
-
- 17. ofstream out( argv[2], ios::binary );
- 18. if( !out )
- 19. {
- 20. print_error( "不能打开文件: ", argv[2] );
- 21. }
-
- 22. char ch;
- 23. while( in.get(ch) )
- 24. {
- 25. out.put(ch);
- 26. }
-
- 27. if( !in.eof() )
- 28. {
- 29. print_error( "一些莫名其妙的问题!" );
- 30. } return 0;
- 31.}
-
- 32.void print_error( const char* p1, const char* p2)
- 33.{
- 34. cerr << p1 << ' ' << p2 << '\n';
- 35. exit(1);
- 36.}
复制代码 |
|