C++ STL ostreambuf_iterator::failed() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do:  Using the C++ ostreambuf_iterator, failed() to test for failure of an insertion into the output stream buffer in C++ programming

To show: How to use the C++ ostreambuf_iterator, failed() to test for failure of an insertion into the output stream buffer in C++ programming

 

// C++ STL ostreambuf_iterator, failed()

#include <iterator>

#include <vector>

#include <iostream>

using namespace std;

 

int main(void)

{

// ostreambuf_iterator for stream cout

ostreambuf_iterator<char> charOut(cout);

 

// assign characters

*charOut = 'T';

charOut ++;

*charOut = '7';

charOut ++;

*charOut = 'R';

 

// print the result

cout<<" are characters output"<<endl;

bool bol = charOut.failed();

if(bol)

cout<<"At least one insertion failed."<<endl;

else

cout<<"No insertions failed."<<endl;

return 0;

}

 

Output examples:

 

T7R are characters output

No insertions failed.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index