C++/File/Array Based IO


Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179

Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из 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;
}