C++/File/Array Based IO
A dynamic output array
<source lang="cpp">
- include <strstream>
- include <iostream>
using namespace std; int main() {
char *p; ostrstream outs; // dynamically allocate array outs << "C++ array-based I/O "; outs << -10 << hex << " "; outs.setf(ios::showbase); outs << 100 << ends; p = outs.str(); // Freeze dynamic buffer and return // pointer to it. cout << p; return 0;
}
</source>
An array-based output stream
<source lang="cpp">
- include <strstream>
- include <iostream>
using namespace std; int main() {
char str[80]; ostrstream outs(str, sizeof(str)); outs << "array-based I/O. "; outs << 1024 << hex << " "; outs.setf(ios::showbase); outs << 100 << " " << 99.789 << ends; cout << str; return 0;
}
</source>
Using Binary I/O with Array-Based Streams
<source lang="cpp">
- include <iostream>
- include <strstream>
using namespace std; int main() {
char *p = "this is a test\1\2\3\4\5\6\7"; istrstream ins(p); char ch; while (!ins.eof()) { // read and display binary info ins.get(ch); cout << hex << (int) ch << " "; } return 0;
}
</source>