String Stream Examples


String Stream I/O

The following shows writing (output) to and reading (input) from a C string.

#include <fstream.hfa>
#include <strstream.hfa>

int main() {
	enum { size = 256 };
	char buf[size]; // output buffer
	ostrstream osstr = { buf, size }; // bind output buffer/size
	int i = 3, j = 5, k = 7;
	double x = 12345678.9, y = 98765.4321e-11;

	osstr | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc";
	write( osstr ); // write string to stdout
	printf( "%s", buf ); // same lines of output
	sout | i | hex(j) | wd(10, k) | sci(x) | unit(eng(y)) | "abc";

	char buf2[] = "12 14 15 3.5 7e4 abc"; // input buffer
	istrstream isstr = { buf2 };
	char s[10];
	isstr | i | j | k | x | y | s;
	sout  | i | j | k | x | y | s;
}

3␣0x5␣␣␣␣␣␣␣␣␣␣7␣1.234568e+07␣987.654n␣abc
3␣0x5␣␣␣␣␣␣␣␣␣␣7␣1.234568e+07␣987.654n␣abc
3␣0x5␣␣␣␣␣␣␣␣␣␣7␣1.234568e+07␣987.654n␣abc
12␣14␣15␣3.5␣70000.␣abc