[comp.lang.c++] question on stream.h

jsc@athena.mit.edu (Jin S Choi) (01/30/91)

I'm just starting to learn c++ and I was just wondering, is it normal for programs using stream.h to compile much larger than stdio.h? The stream.h equivalent to an 8K program is around 150K. I took a look at stream.h and it #includes just about everything else. Isn't this just a bit inefficient?
-- 

Jin Choi
jsc@athena.mit.edu
617-232-3257

jimad@microsoft.UUCP (Jim ADCOCK) (02/07/91)

In article <1991Jan30.091818.21172@athena.mit.edu> jsc@athena.mit.edu (Jin S Choi) writes:
|I'm just starting to learn c++ and I was just wondering, is it normal for programs using stream.h to compile much larger than stdio.h? The stream.h equivalent to an 8K program is around 150K. I took a look at stream.h and it #includes just about everything else. Isn't this just a bit inefficient?

Yes, and yes.

One common trick is to make a trivial stream work-alike class that is 
only just "good enough" to meet a particular program's needs.  Alternatively,
old time C hacks just continue to use stdio.  Of course, neither streams
nor stdio are good enough to meet the needs of serious program development.



extern "C"
{
#include <stdio.h>
}

typedef char* cstr;

class tinystream
{
private:
	FILE* fp;
public:
	tinystream(FILE* p) : fp(p)      { }
	tinystream& operator<<(char c)   { putc(c, fp); return *this; }
	tinystream& operator<<(long l)   { fprintf(fp, "%ld",l); return *this; }
	tinystream& operator<<(int i)    { fprintf(fp, "%d",i); return *this; }
	tinystream& operator<<(double d) { fprintf(fp, "%g",d); return *this; }
	tinystream& operator<<(cstr s)   { fputs(s, fp); return *this; }
	tinystream& operator>>(char& c)  { c = fgetc(fp); return *this; }
	tinystream& operator>>(long& l)  { fscanf(fp, "%ld",&l); return *this; }
	tinystream& operator>>(int& i)   { fscanf(fp, "%d",&i); return *this; }
	tinystream& operator>>(double& d){ fscanf(fp, "%lg",&d); return *this; }
	tinystream& operator>>(float& f) { fscanf(fp, "%f",&f); return *this; }
};

tinystream cin(stdin);
tinystream cout(stdout);

typedef tinystream stream;

// ---------

main()
{
	char c; 
	cout << "\ntype a char:\n"; cin >> c; cout << "\nyou typed: " << c << '\n';
	
	long l; 
	cout << "\ntype a long:\n"; cin >> l; cout << "\nyou typed: " << l << '\n';
		
	int i; 
	cout << "\ntype an int:\n"; cin >> i; cout << "\nyou typed: " << i << '\n';
	
	double d; 
	cout << "\ntype a double:\n"; cin >> d; cout << "\nyou typed: " << d << '\n';
	
	float f;
	cout << "\ntype a float:\n"; cin >> f; cout << "\nyou typed: " << f << '\n';
	
	return 0;	
}