Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
ifstream.cpp
Go to the documentation of this file.
1
29#include <fstream> // for fstream
30#include <iomanip> // for setw
31#include <ios> // for dec, hex, oct, right, showbase
32#include <iostream> // for cerr, cout
33#include <locale> // for isspace()
34
35#include <examples.h>
36
37
38int main ( )
39{
40 const char ntbs[] = "Le minot passait la piece a frotter.";
41
42 try {
43
44 // create a read/write file-stream object
45 std::ifstream in ("ifstream.results.out",
46 std::ios::in | std::ios::out | std::ios::trunc);
47
48 if (!in.is_open ())
49 throw (std::ios::failure ("open error"));
50
51 // associate the ostream object's buffer with the ifstream object
52 std::ostream out (in.rdbuf ());
53
54 // output ntbs in out
55 out << ntbs << '\n';
56
57 // seek to the beginning of the file
58 in.seekg (0);
59
60 char c;
61
62 // output each space-separated word on a separate line
63 while (in.get (c)) {
64 if ((std::isspace)(c, in.getloc ()))
65 std::cout << '\n';
66 else
67 std::cout << c;
68 }
69
70 std::cout << '\n';
71
72 // clear stream state, failbit | eofbit set by last call to get ()
73 in.clear ();
74
75 // move back to the beginning of the file
76 in.seekg (0);
77
78 char buf [40];
79
80 // guard against buffer overflow
81 in.width (sizeof buf);
82
83 // set right justification
84 std::cout << std::right;
85
86 // does the same thing as the previous code
87 // output each word on a separate line in a field of width 10
88 while (in >> buf) {
89 std::cout.width (10);
90 std::cout << buf << '\n';
91 }
92
93 std::cout << '\n';
94
95 // clear the stream state
96 in.clear ();
97
98 // output the base info before each integer
99 out << std::showbase;
100
101 std::ostream::pos_type pos= out.tellp ();
102
103 const long x = 10;
104
105 // output x in hex with a field with of 10
106 out << std::hex << std::setw (10) << x << '\n';
107
108 // output x in oct with a field with of 10
109 out << std::oct << std::setw (10) << x << '\n';
110
111 // output x in dec with a field with of 10
112 out << std::dec << std::setw (10) << x << '\n';
113
114 // move back to the beginning of the file
115 in.seekg (0);
116
117 // output the entire file
118 std::cout << in.rdbuf () << '\n';
119
120 // clear the stream state
121 in.clear ();
122
123 // seek the input sequence to pos
124 in.seekg (pos);
125
126 long a = 0;
127 long b = 0;
128 long d = 0;
129
130 in.unsetf (std::ios::basefield);
131
132 // read the previous outputted integer
133 in >> a >> b >> d;
134
135 // output 3 times 10
136 std::cout << a << '\n' << b << '\n' << d << '\n';
137 }
138 catch (std::ios::failure e) {
139 std::cerr << e.what () << '\n';
140 }
141
142 return 0;
143}
#define d
Definition 17.names.cpp:115
#define e
Definition 17.names.cpp:118
#define x
Definition 17.names.cpp:140
#define a
Definition 17.names.cpp:109
#define buf
Definition 17.names.cpp:452
#define c
Definition 17.names.cpp:111
#define b
Definition 17.names.cpp:110
#define pos
Definition 17.names.cpp:518
const int out
const int in
int main()
Definition ifstream.cpp:38