Metadata
Title
String Stream Examples
Category
general
UUID
9281e042fe454b89883cce52a0f842b7
Source URL
https://cforall.uwaterloo.ca/features/strstream.shtml
Parent URL
https://cforall.uwaterloo.ca/features/
Crawl Time
2026-03-18T05:16:21+00:00
Rendered Raw Markdown

String Stream Examples

Source: https://cforall.uwaterloo.ca/features/strstream.shtml Parent: https://cforall.uwaterloo.ca/features/


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