鱼C论坛

 找回密码
 立即注册
查看: 2898|回复: 4

C++程序求详细注视

[复制链接]
发表于 2012-3-1 23:39:36 | 显示全部楼层 |阅读模式
2鱼币
求详细注视

  1. #include <fstream>
  2. #include <cstdlib>
  3. #include <iostream>  
  4. using namespace std;  
  5. void print_error( const char*, const char* = "" );  
  6. int main( int argc, char* argv[] )
  7. {      
  8.     if( argc != 3 )      
  9.     {            
  10.         print_error( "输入形式: copyFile 源文件名 目标文件名 \n" );      
  11.     }              
  12.     ifstream in( argv[1], ios::binary );            
  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. }
复制代码

最佳答案

查看完整内容

ios为基类 派生了读写文件类 argv命令行参数 最后个判断是废话
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-3-1 23:39:37 | 显示全部楼层
ios为基类
派生了读写文件类
argv命令行参数
最后个判断是废话
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-2 22:11:49 | 显示全部楼层
一句话是用来命令行参数,来复制文件
详细注视:
  1. 01.#include <fstream> //C++文件流类头文件

  2. 02.#include <cstdlib>

  3. 03.#include <iostream>  

  4. 04.using namespace std;  

  5. 05.void print_error( const char*, const char* = "" );  

  6. 06.int main( int argc, char* argv[] )

  7. 07.{      

  8. 08.    if( argc != 3 )       //Main函数的第一个参数是参数的个数,如果不是三个就错误

  9. 09.    {            

  10. 10.        print_error( "输入形式: copyFile 源文件名 目标文件名 \n" );      

  11. 11.    }              

  12. 12.    ifstream in( argv[1], ios::binary );          //argv是一个参数数组,第二个就是源文件   

  13. 13.    if( !in )      

  14. 14.    {            

  15. 15.        print_error( "不能打开文件: ", argv[1] );      

  16. 16.    }        

  17. 17.    ofstream out( argv[2], ios::binary );            

  18. 18.    if( !out )      

  19. 19.    {            

  20. 20.        print_error( "不能打开文件: ", argv[2] );      

  21. 21.    }              

  22. 22.    char ch;      

  23. 23.    while( in.get(ch) )      

  24. 24.    {            

  25. 25.        out.put(ch);      

  26. 26.    }              

  27. 27.    if( !in.eof() )      

  28. 28.    {            

  29. 29.        print_error( "一些莫名其妙的问题!" );      

  30. 30.    }        return 0;

  31. 31.}  

  32. 32.void print_error( const char* p1, const char* p2)

  33. 33.{      

  34. 34.    cerr << p1 << ' ' << p2 << '\n';      

  35. 35.    exit(1);

  36. 36.}
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2012-3-3 18:50:18 | 显示全部楼层
如果略去错误检查,下面这个写法应该更简洁些
  1. #include <fstream>
  2. #include <cstdlib>
  3. #include <iostream>  
  4. using namespace std;  

  5. int main( int argc, char* argv[] ){
  6.         ofstream( argv[2], ios::binary )<<ifstream( argv[1], ios::binary ).rdbuf();
  7. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-9-8 23:16:09 | 显示全部楼层
很不错呀,多谢分享!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 10:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表