C++/File/Array Based IO

Материал из C\C++ эксперт
Перейти к: навигация, поиск

A dynamic output array

#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;
}


An array-based output stream

#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;
}


Using Binary I/O with Array-Based Streams

#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;
}