Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
istreambuf_iterator.cpp
Go to the documentation of this file.
1
29#include <fstream> // for ofstream, istreambuf_iterator
30#include <iostream> // for cout
31#include <cstdio> // for tmpnam () and remove ()
32
33#include <examples.h>
34
35
36int main ( )
37{
38#ifndef _RWSTD_STRICT_ANSI
39
40 // use an extension of this implementation: NULL file name argument
41 // creates a temporary file; closing the file stream object or its
42 // associated filebuf removes the temporary file
43 const char* const fname = 0;
44
45#else // if defined (_RWSTD_STRICT_ANSI)
46
47 char fnamebuf [L_tmpnam];
48
49 // create a temporary filename
50 const char* const fname = std::tmpnam (fnamebuf);
51
52 if (!fname)
53 return 1;
54
55#endif // _RWSTD_STRICT_ANSI
56
57 // open the file is_iter.out for reading and writing
58 std::ofstream out (fname, std::ios::out | std::ios::in | std::ios::trunc);
59
60 // output the example sentence into the file
61 out << "Ceci est un simple exemple pour demontrer le\n"
62 "fonctionnement de istreambuf_iterator.";
63
64 // seek to the beginning of the file
65 out.seekp (0);
66
67 // construct an istreambuf_iterator pointing to
68 // the ofstream object underlying streambuffer
69 std::istreambuf_iterator<char, std::char_traits<char> > iter (out.rdbuf ());
70
71 // construct an end of stream iterator
72 const std::istreambuf_iterator<char, std::char_traits<char> > end;
73
74 std::cout << '\n';
75
76 // output the content of the file
77 while (!iter.equal (end)) {
78
79 // use both operator++ and operator*
80 std::cout << *iter++;
81 }
82
83 std::cout << '\n';
84
85 if (fname) {
86 // close the stream before removing the file
87 out.close ();
88
89 // remove the temporary file (must have a name)
90 std::remove (fname);
91 }
92
93 return 0;
94}
const int end
const int out
#define L_tmpnam
int main()