鱼C论坛

 找回密码
 立即注册
查看: 3086|回复: 7

[技术交流] iostream的简单实现

[复制链接]
发表于 2014-2-4 15:59:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 andalousie 于 2014-3-5 12:43 编辑

ios.h
  1. #ifndef IOS_H
  2. #define IOS_H

  3. #include <stdio.h>

  4. namespace std{
  5.         class ostream{
  6.         public:
  7.                 void flush() { if(stdout) fflush(stdout); }
  8.                 ostream& operator << (ostream& (__cdecl *m)(ostream&));
  9.                
  10.                 ostream& __cdecl operator << (int);
  11.                 ostream& __cdecl operator << (char);
  12.                 ostream& __cdecl operator << (double);
  13.                 ostream& __cdecl operator << (long);
  14.                 ostream& __cdecl operator << (unsigned);
  15.                 ostream& __cdecl operator << (unsigned long);
  16.                 ostream& __cdecl operator << (char *);
  17.                 ostream& __cdecl operator << (const char *);
  18.                 ostream& __cdecl operator << (const void *);
  19.         };

  20.         ostream& ostream::operator <<(ostream& (__cdecl *m)(ostream&))
  21.         {
  22.                 return ((*m)(*this));
  23.         }
  24.        
  25.         ostream& __cdecl ostream::operator << (int val)
  26.         {
  27.                 printf("%d", val);
  28.                 return *this;
  29.         }
  30.        
  31.         ostream& __cdecl ostream::operator << (char ch)
  32.         {
  33.                 printf("%c", ch);
  34.                 return *this;
  35.         }
  36.        
  37.         ostream& __cdecl ostream::operator << (double val)
  38.         {
  39.                 printf("%g", val);
  40.                 return *this;
  41.         }
  42.        
  43.         ostream& __cdecl ostream::operator << (long val)
  44.         {
  45.                 printf("%I64d", val);
  46.                 return *this;
  47.         }
  48.        
  49.         ostream& __cdecl ostream::operator << (unsigned val)
  50.         {
  51.                 printf("%u", val);
  52.                 return *this;
  53.         }
  54.        
  55.         ostream& __cdecl ostream::operator << (unsigned long val)
  56.         {
  57.                 printf("%I64u", val);
  58.                 return *this;
  59.         }
  60.        
  61.         ostream& __cdecl ostream::operator << (char * str)
  62.         {
  63.                 printf("%s", str);
  64.                 return *this;
  65.         }
  66.        
  67.         ostream& __cdecl ostream::operator << (const char * str)
  68.         {
  69.                 printf("%s", str);
  70.                 return *this;
  71.         }
  72.        
  73.         ostream& __cdecl ostream::operator << (const void * p)
  74.         {
  75.                 printf("0x%x", p);
  76.                 return *this;
  77.         }
  78.        
  79.         ostream cout;
  80.         ostream cerr;
  81.         ostream& __cdecl flush(ostream& os) { os.flush(); return os; }
  82.         ostream& __cdecl endl(ostream& os) { os << '\n'; os.flush(); return os; }
  83.        
  84.         class istream{
  85.                 ostream *os;
  86.         public:
  87.                 istream& __cdecl operator >> (int&);
  88.                 istream& __cdecl operator >> (char&);
  89.                 istream& __cdecl operator >> (float&);
  90.                 istream& __cdecl operator >> (double&);
  91.                 istream& __cdecl operator >> (char *);
  92.                 int __cdecl get();
  93.                 istream& __cdecl get (char& c);
  94.                 istream& __cdecl getline (char* s, int n);
  95.         };
  96.        
  97.         istream& __cdecl istream::operator >> (int& val)
  98.         {
  99.                 os->flush();
  100.                 scanf("%d", &val);
  101.                 return *this;
  102.         }
  103.        
  104.         istream& __cdecl istream::operator >> (char& ch)
  105.         {
  106.                 os->flush();
  107.                 scanf("%c", &ch);
  108.                 return *this;
  109.         }
  110.        
  111.         istream& __cdecl istream::operator >> (float& val)
  112.         {
  113.                 os->flush();
  114.                 scanf("%f", &val);
  115.                 return *this;
  116.         }
  117.        
  118.         istream& __cdecl istream::operator >> (double& val)
  119.         {
  120.                 os->flush();
  121.                 scanf("%lf", &val);
  122.                 return *this;
  123.         }
  124.        
  125.         istream& __cdecl istream::operator >> (char * str)
  126.         {
  127.                 os->flush();
  128.                 gets(str);
  129.                 return *this;
  130.         }
  131.        
  132.         int __cdecl istream::get()
  133.         {
  134.                 return getchar();
  135.         }
  136.        
  137.         istream& __cdecl istream::get (char& c)
  138.         {
  139.                 os->flush();
  140.                 c = getchar();
  141.                 return *this;
  142.         }
  143.        
  144.         istream& __cdecl istream::getline (char* s, int n)
  145.         {
  146.             int c, i;
  147.             for(i = 0; i < n-1 && (c=getchar())!=EOF && c !='\n'; ++i)
  148.                 s[i] = c;
  149.             if(c == '\n')
  150.                 s[i++]=c;
  151.             s[i] = '\0';
  152.             return *this;
  153.         }
  154.        
  155.         istream cin;
  156. }

  157. #endif
复制代码
做了改动,增加了ostream引用指针的inserter,于是可以大胆用std::cout << std::flush了。Last modified at 12:40, 03-05

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-2-6 18:03:06 | 显示全部楼层
第一个更新版本,支持下我呦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-7 10:49:48 | 显示全部楼层
路过看看= =:ton:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-15 20:28:39 | 显示全部楼层
我只是路过打酱油的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-15 21:22:53 | 显示全部楼层
用printf没什么,自己创建一个输出流,一个缓冲区,才算真本事.而且这个ostream类只实现了真正类库中的ostream功能的冰山一角.有本事不用cout写一个printf看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-2-16 10:19:43 | 显示全部楼层

说得好。我现在还没这个本事。只是因为感到ostream实现的不够直接,才这么写的。谢谢赐教。我也曾经打算写缓冲区的。如果有相关课程,欢迎提供。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2014-2-16 10:40:31 | 显示全部楼层
andalousie 发表于 2014-2-16 10:19
说得好。我现在还没这个本事。只是因为感到ostream实现的不够直接,才这么写的。谢谢赐教。我也曾经打算写 ...

ISO C标准库对文件的读写使用系统调用read/write,为了减少read/write使用次数,自己维护了一个缓冲区。每次读文件的时候先将足够的数据读入到缓冲区,然后从缓冲区读取数据,(比如每次读取一行数据,可以先将n行读取到缓冲区,如果每次从缓冲区读取一行,这样可以大大降低系统调用次数,系统调用对资源耗费相对很大)。写入文件时,可以主动写入(flush),也可以文件关闭时写入,均是将缓冲区的数据写入到文件。
整个数据流程就是  文件 -- 缓冲区(库函数来维护) -- buffer(用户使用的空间)。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-3-5 12:46:05 | 显示全部楼层
再次做了修改,实现了真正的std::endl,而不是简单的当作'\n'字符输出了。{:7_155:}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 03:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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