-
Notifications
You must be signed in to change notification settings - Fork 4
/
iostream.cpp
87 lines (75 loc) · 1.41 KB
/
iostream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "minicrt.h"
#include "iostream"
#ifdef WIN32
#include <Windows.h>
#endif // WIN32
namespace std{
stdout_stream::stdout_stream() : ofstream()
{
fp = stdout;
}
stdout_stream cout;
ofstream::ofstream() : fp(0)
{
}
ofstream::ofstream(const char *filename, ofstream::openmode md) : fp(0)
{
open(filename, md);
}
ofstream::~ofstream()
{
close();
}
ofstream& ofstream::operator<<(char c)
{
fputc(c, fp);
return *this;
}
ofstream& ofstream::operator<<(int n)
{
fprintf(fp, "%d", n);
return *this;
}
ofstream& ofstream::operator<<(const char* str)
{
fprintf(fp, "%s", str);
return *this;
}
ofstream& ofstream::operator<<(ofstream& (*manip)(ofstream&))
{
return manip(*this);
}
void ofstream::open(const char* filename, ofstream::openmode md)
{
char mode[4];
close();
switch (md)
{
case out|trunc:
strcpy(mode, "w+");
break;
case out | in | trunc:
strcpy(mode, "w+");
break;
case out | trunc | binary:
strcpy(mode, "wb");
break;
case out | in | trunc | binary:
strcpy(mode, "wb+");
}
fp = fopen(filename, mode);
}
void ofstream::close()
{
if(fp)
{
fclose(fp);
fp = 0;
}
}
ofstream& ofstream::write(const char* buf, unsigned size)
{
fwrite(buf, 1, size, fp);
return *this;
}
}