2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 // $XConsortium: core_fstream.C /main/4 1996/10/04 10:47:25 drk $
28 void filebuf_exam_test1()
30 fprintf(stderr, "fstream test1 : read\n");
31 fstream fb("stream_test.read", ios::in|ios::out);
34 for ( int i=0; i<2; i++ ) {
36 fprintf(stderr, "buf[0]=%c\n", buf[0]);
37 fprintf(stderr, "buf[1]=%c\n", buf[1]);
38 fprintf(stderr, "buf[2]=%c\n", buf[2]);
39 fprintf(stderr, "buf[3]=%c\n", buf[3]);
41 fprintf(stderr, "gcount=%d\n", fb.gcount());
45 void filebuf_exam_test2()
47 fprintf(stderr, "fstream test2 : write \n");
48 fstream fb("/tmp/stream_test.write", ios::in|ios::out|ios::trunc);
49 char* buf = "abcdefghijklmnopq";
51 for ( int i=0; i<2; i++ ) {
55 system("cmp stream_test.write /tmp/stream_test.write");
56 system("rm /tmp/stream_test.write");
59 void filebuf_exam_test3()
61 fprintf(stderr, "fstream test3 : getline\n");
62 fstream fb("stream_test.getline", ios::in|ios::out);
65 while ( fb.getline(buf, 100) ) {
66 fprintf(stderr, "buf=%s\n", buf);
67 fprintf(stderr, "gcount=%d\n", fb.gcount());
71 void filebuf_exam_test4()
73 fprintf(stderr, "fstream test4 : word counting\n");
74 fstream fb("stream_test.mobydick", ios::in);
78 int curlen, max = -1, cnt = 0;
84 memcpy(largest, buf, strlen(buf));
87 fprintf(stderr, "# of words read is = %d\n", cnt);
88 fprintf(stderr, "the longest word has a length of = %d\n", max);
89 fprintf(stderr, "the longest word is = %s\n", largest);
92 void filebuf_exam_test5()
94 fprintf(stderr, "fstream test 5: (extraction)\n");
95 fstream fb("stream_test.extraction", ios::in|ios::out);
103 // int long char (u)short (u)int
105 fb >> x; fprintf(stderr, "x=%d\n", x);
106 fprintf(stderr, "gcount=%d\n", fb.gcount());
108 fb >> y; fprintf(stderr, "y=%ld\n", y);
109 fprintf(stderr, "gcount=%d\n", fb.gcount());
111 fb >> c; fprintf(stderr, "c=%c\n", c);
112 fprintf(stderr, "gcount=%d\n", fb.gcount());
114 fb >> u; fprintf(stderr, "u=%d\n", u);
115 fprintf(stderr, "gcount=%d\n", fb.gcount());
117 fb >> v; fprintf(stderr, "v=%d\n", v);
118 fprintf(stderr, "gcount=%d\n", fb.gcount());
122 void filebuf_exam_test6()
124 fprintf(stderr, "fstream test 6: (mixed insertion and extraction [1]) \n");
126 system("cp stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
128 fstream fb("/tmp/stream_test.mixed_ins_extr", ios::in|ios::out);
136 // int long char (u)short (u)int
138 fb >> x; fprintf(stderr, "x=%d\n", x);
139 fb >> y; fprintf(stderr, "y=%ld\n", y);
144 system("diff stream_test.mixed_ins_extr /tmp/stream_test.mixed_ins_extr");
147 void filebuf_exam_test7()
149 fprintf(stderr, "fstream test 7: (mixed insertion and extraction [2]) \n");
151 system("cp stream_test.mixed_ins_extr.2 /tmp/stream_test.mixed_ins_extr.2");
152 fstream fb("/tmp/stream_test.mixed_ins_extr.2", ios::in|ios::out|ios::trunc);
158 fb << x << y << "abcdefghijk";
160 fb.seekg(9, ios::beg);
162 fb >> c; fprintf(stderr, "c=%c\n", c);
163 fb >> c; fprintf(stderr, "c=%c\n", c);
165 system("diff /tmp/stream_test.mixed_ins_extr.2 stream_test.mixed_ins_extr.2");
166 system("rm /tmp/stream_test.mixed_ins_extr.2");
169 void filebuf_exam_test8()
171 fprintf(stderr, "fstream test 8: (seek and read) \n");
173 fstream fb("stream_test.seek_and_read", ios::in|ios::out);
175 fb.seekg(10, ios::beg);
178 fb >> c; fprintf(stderr, "c=%c\n", c);
179 fb >> c; fprintf(stderr, "c=%c\n", c);
181 fb.seekg(20, ios::beg);
182 fb >> c; fprintf(stderr, "c=%c\n", c);
183 fb >> c; fprintf(stderr, "c=%c\n", c);
186 void filebuf_exam_test9()
188 fprintf(stderr, "fstream test 9: (append) \n");
190 system("rm -f /tmp/stream_test.append");
191 fstream fb("/tmp/stream_test.append", ios::app);
193 fb << "lseek() sets the seek";
194 fb << " pointer associated with the open";
196 system("diff /tmp/stream_test.append stream_test.append");
197 system("rm /tmp/stream_test.append");
200 void filebuf_exam_test10()
202 fprintf(stderr, "fstream test 10: (cerr) \n");
206 fb << "cerr: lseek() sets the seek\n";
207 fb << "cerr: pointer associated with the open\n";
210 fb1 << "cout::lseek() sets the seek\n";
211 fb1 << "cout:: pointer associated with the open\n";
214 void filebuf_exam_test11(char* nm)
216 fprintf(stderr, "fstream test11 : getline (2)\n");
217 fstream fb(nm, ios::in);
220 while ( fb.getline(buf, 100) ) {
221 fprintf(stderr, "buf=%s\n", buf);
222 fprintf(stderr, "gcount=%d\n", fb.gcount());
226 void filebuf_exam_test12()
228 fprintf(stderr, "fstream test12 : write (2)\n");
229 fstream fb("/tmp/stream_test.write", ios::out);
230 char* buf = "abcdefghijklmnopq";
232 for ( int i=0; i<2; i++ ) {
233 fb.write(buf+i*4, 4);
235 system("diff stream_test.write /tmp/stream_test.write");
236 system("rm /tmp/stream_test.write");
239 void filebuf_exam_test13(char* nm, int pos)
241 fprintf(stderr, "fstream test13 : read (2)\n");
242 fstream fb(nm, ios::in);
245 fb.seekg(pos, ios::beg);
250 fprintf(stderr, "len=%d\n", len);
251 fprintf(stderr, "gcount=%d\n", fb.gcount());
254 void filebuf_exam_test14(char* nm)
256 fprintf(stderr, "fstream test14 : write (3)\n");
257 fstream fb(nm, ios::out);
258 char* buf = "abcdefghijklmnopq";
260 for ( int i=0; i<2; i++ ) {
261 fb.write(buf+i*4, 4);
266 void usage(char** argv)
268 fprintf(stderr, "Usage: %s option\n", argv[0]);
269 fprintf(stderr, "option = 1: read\n");
270 fprintf(stderr, " 2: write\n");
271 fprintf(stderr, " 3: getline\n");
272 fprintf(stderr, " 4: word counting\n");
273 fprintf(stderr, " 5: extraction\n");
274 fprintf(stderr, " 6: mixed insertion/extraction [1]\n");
275 fprintf(stderr, " 7: mixed insertion/extraction [2]\n");
276 fprintf(stderr, " 8: seek and read\n");
277 fprintf(stderr, " 9: append\n");
278 fprintf(stderr, " 10: cerr\n");
279 fprintf(stderr, " 11: getline (2). arguments: 11 filename\n");
280 fprintf(stderr, " 12: write (2)\n");
281 fprintf(stderr, " 13: read (2). arg: 13 filename offset\n");
282 fprintf(stderr, " 14: write (3). arg: 14 filename\n");
285 int main(int argc, char** argv)
292 int i = atoi(argv[1]);
295 filebuf_exam_test1();
298 filebuf_exam_test2();
301 filebuf_exam_test3();
304 filebuf_exam_test4();
307 filebuf_exam_test5();
310 filebuf_exam_test6();
313 filebuf_exam_test7();
316 filebuf_exam_test8();
319 filebuf_exam_test9();
322 filebuf_exam_test10();
326 filebuf_exam_test11(argv[2]);
331 filebuf_exam_test12();
335 filebuf_exam_test13(argv[2], atoi(argv[3]));
339 filebuf_exam_test14(argv[2]);
342 filebuf_exam_test1();
343 filebuf_exam_test2();
344 filebuf_exam_test3();
345 filebuf_exam_test4();
346 filebuf_exam_test5();
347 filebuf_exam_test6();
348 filebuf_exam_test7();
349 filebuf_exam_test8();
350 filebuf_exam_test9();
351 filebuf_exam_test10();
352 filebuf_exam_test12();