Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
filebuf.cpp
Go to the documentation of this file.
1
29#include <fstream> // for filebuf, ifstream, istream
30#include <iostream> // for cout, endl
31#include <cstdio> // for tmpnam(), remove()
32
33#include <examples.h>
34
35int main ()
36{
37#ifndef _RWSTD_NO_EXT_FILEBUF
38
39 // use an extension of this implementation: NULL file name argument
40 // creates a temporary file; closing the file stream object or its
41 // associated filebuf removes the temporary file
42 const char* const fname = 0;
43
44#else // if defined (_RWSTD_NO_EXT_FILEBUF)
45
46 char fnamebuf [L_tmpnam];
47
48 // create a temporary filename
49 const char* const fname = std::tmpnam (fnamebuf);
50
51 if (!fname)
52 return 1;
53
54#endif // _RWSTD_NO_EXT_FILEBUF
55
56 // create a filebuf object for reading and writing of a temporary file
57 std::filebuf outbuf;
58
59 outbuf.open (fname, std::ios::in | std::ios::out | std::ios::trunc);
60
61 // set the internal character buffer size
62 // buffer will be allocated internally and deallocated in object dtor
63 outbuf.pubsetbuf (0, 256);
64
65 // associate the filebuf object with an iostream object
66 std::iostream out (&outbuf);
67
68 // open this source code file and output it to the temporary file
69 out << std::ifstream (__FILE__).rdbuf ();
70
71 // seek to the beginning of the stream
72 out.seekp (0);
73
74 // output the contents of the `out' object to standard output
75 std::cout << out.rdbuf ();
76
77 // close the filebuf object before removing the underlying file
78 outbuf.close ();
79
80 if (fname) {
81 // remove the temporary file if it has a name
82 // otherwise, if fname is NULL, the temporary file will
83 // have already been automatically removed by the call
84 // to close() above
85 std::remove (fname);
86 }
87
88 return 0;
89}
const int out
#define L_tmpnam
int main()
Definition filebuf.cpp:35