Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
ostreambuf_iterator.cpp
Go to the documentation of this file.
1
29#include <fstream> // for filebuf
30#include <iostream> // for cout
31#include <iterator> // for streambuf_iterator's
32#include <cstdio> // for L_tmpnam, tmpnam and remove
33
34#include <examples.h>
35
36
37int main ( )
38{
39#ifndef _RWSTD_STRICT_ANSI
40
41 // use an extension of this implementation: NULL file name argument
42 // creates a temporary file; closing the file stream object or its
43 // associated filebuf removes the temporary file
44 const char* const fname = 0;
45
46#else // if defined (_RWSTD_STRICT_ANSI)
47
48 char fnamebuf [L_tmpnam];
49
50 // create a temporary filename
51 const char* const fname = std::tmpnam (fnamebuf);
52
53 if (!fname)
54 return 1;
55
56#endif // _RWSTD_STRICT_ANSI
57
58 // create a filebuf object
59 std::filebuf buf;
60
61 // open the file and link it to the filebuf object
62 buf.open (fname, std::ios::in | std::ios::out | std::ios::trunc);
63
64 // create an ostreambuf_iterator and link it to
65 // the filebuf object
66 std::ostreambuf_iterator<char, std::char_traits<char> > out_iter (&buf);
67
68 const char charset[] =
69 "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
70
71 // output characters into the file using the ostreambuf_iterator
72 for (unsigned i = 0; charset [i]; ++i)
73 out_iter = charset [i];
74
75 // seek to the beginning of the file
76 buf.pubseekpos(0);
77
78 // create an istreambuf_iterator and link it to the filebuf object
79 std::istreambuf_iterator<char, std::char_traits<char> > in_iter (&buf);
80
81 // construct an end of stream iterator
82 std::istreambuf_iterator<char, std::char_traits<char> > end;
83
84 std::cout << '\n';
85
86 // output the contents of the file
87 while (!in_iter.equal (end))
88 std::cout << *in_iter++;
89
90 std::cout << '\n';
91
92 if (fname) {
93 // close the stream before removing the file
94 buf.close ();
95
96 // remove the temporary file (must have a name)
97 std::remove (fname);
98 }
99
100 return 0;
101}
#define i
Definition 17.names.cpp:126
#define buf
Definition 17.names.cpp:452
const int end
#define L_tmpnam
int main()