iostream的简单实现
本帖最后由 andalousie 于 2014-3-5 12:43 编辑ios.h#ifndef IOS_H
#define IOS_H
#include <stdio.h>
namespace std{
class ostream{
public:
void flush() { if(stdout) fflush(stdout); }
ostream& operator << (ostream& (__cdecl *m)(ostream&));
ostream& __cdecl operator << (int);
ostream& __cdecl operator << (char);
ostream& __cdecl operator << (double);
ostream& __cdecl operator << (long);
ostream& __cdecl operator << (unsigned);
ostream& __cdecl operator << (unsigned long);
ostream& __cdecl operator << (char *);
ostream& __cdecl operator << (const char *);
ostream& __cdecl operator << (const void *);
};
ostream& ostream::operator <<(ostream& (__cdecl *m)(ostream&))
{
return ((*m)(*this));
}
ostream& __cdecl ostream::operator << (int val)
{
printf("%d", val);
return *this;
}
ostream& __cdecl ostream::operator << (char ch)
{
printf("%c", ch);
return *this;
}
ostream& __cdecl ostream::operator << (double val)
{
printf("%g", val);
return *this;
}
ostream& __cdecl ostream::operator << (long val)
{
printf("%I64d", val);
return *this;
}
ostream& __cdecl ostream::operator << (unsigned val)
{
printf("%u", val);
return *this;
}
ostream& __cdecl ostream::operator << (unsigned long val)
{
printf("%I64u", val);
return *this;
}
ostream& __cdecl ostream::operator << (char * str)
{
printf("%s", str);
return *this;
}
ostream& __cdecl ostream::operator << (const char * str)
{
printf("%s", str);
return *this;
}
ostream& __cdecl ostream::operator << (const void * p)
{
printf("0x%x", p);
return *this;
}
ostream cout;
ostream cerr;
ostream& __cdecl flush(ostream& os) { os.flush(); return os; }
ostream& __cdecl endl(ostream& os) { os << '\n'; os.flush(); return os; }
class istream{
ostream *os;
public:
istream& __cdecl operator >> (int&);
istream& __cdecl operator >> (char&);
istream& __cdecl operator >> (float&);
istream& __cdecl operator >> (double&);
istream& __cdecl operator >> (char *);
int __cdecl get();
istream& __cdecl get (char& c);
istream& __cdecl getline (char* s, int n);
};
istream& __cdecl istream::operator >> (int& val)
{
os->flush();
scanf("%d", &val);
return *this;
}
istream& __cdecl istream::operator >> (char& ch)
{
os->flush();
scanf("%c", &ch);
return *this;
}
istream& __cdecl istream::operator >> (float& val)
{
os->flush();
scanf("%f", &val);
return *this;
}
istream& __cdecl istream::operator >> (double& val)
{
os->flush();
scanf("%lf", &val);
return *this;
}
istream& __cdecl istream::operator >> (char * str)
{
os->flush();
gets(str);
return *this;
}
int __cdecl istream::get()
{
return getchar();
}
istream& __cdecl istream::get (char& c)
{
os->flush();
c = getchar();
return *this;
}
istream& __cdecl istream::getline (char* s, int n)
{
int c, i;
for(i = 0; i < n-1 && (c=getchar())!=EOF && c !='\n'; ++i)
s = c;
if(c == '\n')
s=c;
s = '\0';
return *this;
}
istream cin;
}
#endif做了改动,增加了ostream引用指针的inserter,于是可以大胆用std::cout << std::flush了。Last modified at 12:40, 03-05
第一个更新版本,支持下我呦 路过看看= =:ton: 我只是路过打酱油的 用printf没什么,自己创建一个输出流,一个缓冲区,才算真本事.而且这个ostream类只实现了真正类库中的ostream功能的冰山一角.有本事不用cout写一个printf看看 柠“萌”圆 发表于 2014-2-15 21:22 static/image/common/back.gif
用printf没什么,自己创建一个输出流,一个缓冲区,才算真本事.而且这个ostream类只实现了真正类库中的ostream ...
说得好。我现在还没这个本事。只是因为感到ostream实现的不够直接,才这么写的。谢谢赐教。我也曾经打算写缓冲区的。如果有相关课程,欢迎提供。 andalousie 发表于 2014-2-16 10:19 static/image/common/back.gif
说得好。我现在还没这个本事。只是因为感到ostream实现的不够直接,才这么写的。谢谢赐教。我也曾经打算写 ...
ISO C标准库对文件的读写使用系统调用read/write,为了减少read/write使用次数,自己维护了一个缓冲区。每次读文件的时候先将足够的数据读入到缓冲区,然后从缓冲区读取数据,(比如每次读取一行数据,可以先将n行读取到缓冲区,如果每次从缓冲区读取一行,这样可以大大降低系统调用次数,系统调用对资源耗费相对很大)。写入文件时,可以主动写入(flush),也可以文件关闭时写入,均是将缓冲区的数据写入到文件。
整个数据流程就是文件 -- 缓冲区(库函数来维护) -- buffer(用户使用的空间)。 再次做了修改,实现了真正的std::endl,而不是简单的当作'\n'字符输出了。{:7_155:}
页:
[1]