Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
istream1.cpp
Go to the documentation of this file.
1
29#include <fstream> // for basic_ofstream
30#include <iostream> // for basic_istream, cout
31#include <cstdio> // for tmpnam () and remove ()
32
33#include <examples.h>
34
35int main ()
36{
37#ifndef _RWSTD_STRICT_ANSI
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_STRICT_ANSI)
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_STRICT_ANSI
55
56 // typedefs for convenience
57 typedef std::basic_istream<char, std::char_traits<char> > Input;
58 typedef std::basic_ofstream<Input::char_type, Input::traits_type> Output;
59
60 Input::char_type s [200];
61
62 // open a file for read and write operations
63 Output out (fname, std::ios::in | std::ios::out | std::ios::trunc);
64
65 // tie the istream object to the ofstream filebuf
66 Input in (out.rdbuf ());
67
68 double x = 3.14159;
69 int i = 3;
70
71 // output to the file
72 out << "He lifted his head and pondered.\n"
73 << x << '\n' << i << '\n';
74
75 // seek to the beginning of the file
76 in.seekg (0);
77
78 // read from the file using formatted functions
79 in.getline (s, sizeof s);
80 in >> x >> i;
81
82 // seek to the beginning of the file
83 in.seekg (0, std::ios::beg);
84
85 // output the all file to the standard output
86 std::cout << in.rdbuf ();
87
88 // seek to the beginning of the file
89 in.seekg (0);
90
91 // read the first line in the file
92 // "He lifted his head and pondered."
93 in.getline (s, sizeof s / 2);
94
95 std::cout << s << '\n';
96
97 // read the second line in the file
98 in.getline (s, sizeof s / 2);
99
100 std::cout << s << '\n';
101
102 // seek to the beginning of the file
103 in.seekg (0);
104
105 // read the first line in the file
106 // "He lifted his head and pondered."
107 in.get (s, sizeof s / 2);
108
109 // remove the newline character
110 in.ignore ();
111
112 std::cout << s << '\n';
113
114 // read the second line in the file
115 // 3.14159
116 in.get (s, sizeof s / 2);
117
118 std::cout << s << '\n';
119
120 // remove the newline character
121 in.ignore ();
122
123 // store the current file position
124 const Input::pos_type position = in.tellg ();
125
126 out << "\nreplace the int\n";
127
128 // move back to the previous saved position
129 in.seekg (position);
130
131 // for convenience
132 const Input::int_type eof = Input::traits_type::eof ();
133
134 // output the remainder of the file
135 // this is equivalent to "std::cout << in.rdbuf ();"
136 while (!Input::traits_type::eq_int_type (in.peek (), eof))
137 std::cout << Input::traits_type::to_char_type (in.get ());
138
139 std::cout << "\n\n\n";
140
141 if (fname) {
142 // close the stream before removing the file
143 out.close ();
144
145 // remove the temporary file (must have a name)
146 std::remove (fname);
147 }
148
149 return 0;
150}
#define i
Definition 17.names.cpp:126
#define position
Definition 17.names.cpp:519
#define x
Definition 17.names.cpp:140
#define s
const int out
const int in
#define L_tmpnam
int main()
Definition istream1.cpp:35