-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall.html
More file actions
1683 lines (1405 loc) · 44 KB
/
all.html
File metadata and controls
1683 lines (1405 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>C++ I/O</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">C++ I/O</a>
</div>
<div class="name-format">
bad
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
bool bad();
</pre>
<p>The bad() function returns true if a fatal error with the current
stream has occurred, false otherwise.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="eof.html">eof</a><br>
<a href="fail.html">fail</a><br>
<a href="good.html">good</a><br>
<a href="rdstate.html">rdstate</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
clear
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
void clear( iostate flags = ios::goodbit );
</pre>
<p>The function clear() does two things:</p>
<ul>
<li>it clears all <a href="../io_flags.html#state_flags">io stream
state flag</a>s associated with the current stream,</li>
<li>and sets the flags denoted by <em>flags</em></li>
</ul>
<p>The <em>flags</em> argument defaults to ios::goodbit, which means
that by default, all flags will be cleared and ios::goodbit will be
set.</p>
<div class="related-examples-format">
Example code:
</div>
<div class="related-examples">
<p>For example, the following code uses the clear() function to
reset the flags of an output file stream, after an attempt is made
to read from that output stream:</p>
<pre class="prettyprint">
fstream outputFile( "output.txt", fstream::out );
// try to read from the output stream; this shouldn't work
int val;
outputFile >> val;
if( outputFile.fail() ) {
cout << "Error reading from the output stream" << endl;
// reset the flags associated with the stream
outputFile.clear();
}
for( int i = 0; i < 10; i++ ) {
outputFile << i << " ";
}
outputFile << endl;
</pre>
</div>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="eof.html">eof</a><br>
<a href="fail.html">fail</a><br>
<a href="good.html">good</a><br>
<a href="rdstate.html">rdstate</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
close
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
void close();
</pre>
<p>The close() function closes the associated file stream.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="constructors.html">I/O Constructors</a><br>
<a href="open.html">open</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
I/O Constructors
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
fstream( const char *filename, openmode mode );
ifstream( const char *filename, openmode mode );
ofstream( const char *filename, openmode mode );
</pre>
<p>The fstream, ifstream, and ofstream objects are used to do file
I/O. The optional <em>mode</em> defines how the file is to be opened,
according to the <a href="../io_flags.html#mode_flags">io stream mode
flag</a>s. The optional <em>filename</em> specifies the file to be
opened and associated with the stream.</p>
<p>Input and output file streams can be used in a similar manner to
C++ predefined I/O streams, cin and cout.</p>
<div class="related-examples-format">
Example code:
</div>
<div class="related-examples">
<p>The following code reads input data and appends the result to an
output file.</p>
<pre class="prettyprint">
ifstream fin( "/tmp/data.txt" );
ofstream fout( "/tmp/results.txt", ios::app );
while( fin >> temp )
fout << temp + 2 << endl;
fin.close();
fout.close();
</pre>
</div>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="close.html">close</a><br>
<a href="open.html">open</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
eof
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
bool eof();
</pre>
<p>The function eof() returns true if the end of the associated input
file has been reached, false otherwise.</p>
<p>For example, the following code reads data from an input stream
<em>in</em> and writes it to an output stream <em>out</em>, using
eof() at the end to check if an error occurred:</p>
<pre class="prettyprint">
char buf[BUFSIZE];
do {
in.read( buf, BUFSIZE );
std::streamsize n = in.gcount();
out.write( buf, n );
} while( in.good() );
if( in.bad() || !in.eof() ) {
// fatal error occurred
}
in.close();
</pre>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="bad.html">bad</a><br>
<a href="clear.html">clear</a><br>
<a href="fail.html">fail</a><br>
<a href="good.html">good</a><br>
<a href="rdstate.html">rdstate</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
C++ I/O Examples
</div>
<h2>Reading From Files</h2>
<p>Assume that we have a file named <em>data.txt</em> that
contains this text:</p>
<pre class="example-data">
Fry: One Jillion dollars.
[Everyone gasps.]
Auctioneer: Sir, that's not a number.
[Everyone gasps.]
</pre>
<p>We could use this code to read data from the file, word by word:</p>
<pre class="prettyprint">
ifstream fin("data.txt");
string s;
while( fin >> s ) {
cout << "Read from file: " << s << endl;
}
</pre>
<p>When used in this manner, we'll get space-delimited bits of text
from the file:</p>
<pre class="example-output">
Read from file: Fry:
Read from file: One
Read from file: Jillion
Read from file: dollars.
Read from file: [Everyone
Read from file: gasps.]
Read from file: Auctioneer:
Read from file: Sir,
Read from file: that's
Read from file: not
Read from file: a
Read from file: number.
Read from file: [Everyone
Read from file: gasps.]
</pre>
<p>Note that in the previous example, all of the whitespace that
separated words (including newlines) was lost. If we were
interested in preserving whitespace, we could read the file in
line-by-line using the <a href="getline.html">I/O getline() function</a>.</p>
<pre class="prettyprint">
ifstream fin("data.txt");
const int LINE_LENGTH = 100;
char str[LINE_LENGTH];
while( fin.getline(str,LINE_LENGTH) ) {
cout << "Read from file: " << str << endl;
}
</pre>
<p>Reading line-by-line produces the following output:</p>
<pre class="example-output">
Read from file: Fry: One Jillion dollars.
Read from file: [Everyone gasps.]
Read from file: Auctioneer: Sir, that's not a number.
Read from file: [Everyone gasps.]
</pre>
<p>If you want to avoid reading into character arrays, you can use
the <a href="../cppstring/getline.html">C++ string getline()</a> function
to read lines into <a href="../cppstring/index-2.html">strings</a>:</p>
<pre class="prettyprint">
ifstream fin("data.txt");
string s;
while( getline(fin,s) ) {
cout << "Read from file: " << s << endl;
}
</pre>
<h2>Checking For Errors</h2>
<p>Simply evaluating an I/O object in a boolean context will return
false if any errors have occurred:</p>
<pre class="prettyprint">
string filename = "data.txt";
ifstream fin( filename.c_str() );
if( !fin ) {
cout << "Error opening " << filename << " for input" << endl;
exit(-1);
}
</pre>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
fail
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
bool fail();
</pre>
<p>The fail() function returns true if an error has occurred with the
current stream, false otherwise.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="bad.html">bad</a><br>
<a href="clear.html">clear</a><br>
<a href="eof.html">eof</a><br>
<a href="good.html">good</a><br>
<a href="rdstate.html">rdstate</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
fill
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
char fill();
char fill( char ch );
</pre>
<p>The function fill() either returns the current fill character, or
sets the current fill character to <em>ch</em>.</p>
<p>The fill character is defined as the character that is used for
padding when a number is smaller than the specified <a href=
"width.html">width</a>(). The default fill character is the space
character.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="precision.html">precision</a><br>
<a href="width.html">width</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
flags
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
fmtflags flags();
fmtflags flags( fmtflags f );
</pre>
<p>The flags() function either returns the <a href=
"../io_flags.html#format_flags">io stream format flag</a>s for the
current stream, or sets the flags for the current stream to be
<em>f</em>.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="setf.html">setf</a><br>
<a href="unsetf.html">unsetf</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
flush
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
ostream& flush();
</pre>
<p>The flush() function causes the buffer for the current output
stream to be actually written out to the attached device.</p>
<p>This function is useful for printing out debugging information,
because sometimes programs abort before they have a chance to write
their output buffers to the screen. Judicious use of flush() can
ensure that all of your debugging statements actually get
printed.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="put.html">put</a><br>
<a href="write.html">write</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
gcount
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
streamsize gcount();
</pre>
<p>The function gcount() is used with input streams, and returns the
number of characters read by the last input operation.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="get.html">get</a><br>
<a href="getline.html">getline</a><br>
<a href="read.html">read</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
get
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
int get();
istream& get( char& ch );
istream& get( char* buffer, streamsize num );
istream& get( char* buffer, streamsize num, char delim );
istream& get( streambuf& buffer );
istream& get( streambuf& buffer, char delim );
</pre>
<p>The get() function is used with input streams, and either:</p>
<ul>
<li>reads a character and returns that value,</li>
<li>reads a character and stores it as <em>ch</em>,</li>
<li>reads characters into <em>buffer</em> until <em>num</em> - 1
characters have been read, or <strong>EOF</strong> or newline
encountered,</li>
<li>reads characters into <em>buffer</em> until <em>num</em> - 1
characters have been read, or <strong>EOF</strong> or the
<em>delim</em> character encountered (<em>delim</em> is not read
until next time),</li>
<li>reads characters into buffer until a newline or
<strong>EOF</strong> is encountered,</li>
<li>or reads characters into buffer until a newline,
<strong>EOF</strong>, or <em>delim</em> character is encountered
(again, <em>delim</em> isn't read until the next get() ).</li>
</ul>
<p>For example, the following code displays the contents of a file
called temp.txt, character by character:</p>
<pre class="prettyprint">
char ch;
ifstream fin( "temp.txt" );
while( fin.get(ch) )
cout << ch;
fin.close();
</pre>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="gcount.html">gcount</a><br>
<a href="getline.html">getline</a><br>
(C++ Strings) <a href="../cppstring/getline.html">getline</a><br>
<a href="ignore.html">ignore</a><br>
<a href="peek.html">peek</a><br>
<a href="put.html">put</a><br>
<a href="read.html">read</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
getline
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
istream& getline( char* buffer, streamsize num );
istream& getline( char* buffer, streamsize num, char delim );
</pre>
<p>The getline() function is used with input streams, and reads
characters into <em>buffer</em> until either:</p>
<ul>
<li><em>num</em> - 1 characters have been read,</li>
<li>a newline is encountered,</li>
<li>an <strong>EOF</strong> is encountered,</li>
<li>or, optionally, until the character <em>delim</em> is read. The
<em>delim</em> character is not put into buffer.</li>
</ul>
<p>For example, the following code uses the getline function to
display the first 100 characters from each line of a text file:</p>
<pre class="prettyprint">
ifstream fin("tmp.dat");
int MAX_LENGTH = 100;
char line[MAX_LENGTH];
while( fin.getline(line, MAX_LENGTH) ) {
cout << "read line: " << line << endl;
}
</pre>
<p>If you'd like to read lines from a file into <a
href="../cppstring/index-2.html">strings</a> instead of character arrays,
consider using the <a href="../cppstring/getline.html">string
getline</a> function.</p>
<p>Those using a Microsoft compiler may find that getline() reads an
extra character, and should consult the documentation on the <a href=
"http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015">Microsoft
getline bug</a>.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="gcount.html">gcount</a><br>
<a href="get.html">get</a><br>
(C++ Strings) <a href="../cppstring/getline.html">getline</a><br>
<a href="ignore.html">ignore</a><br>
<a href="read.html">read</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
good
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
bool good();
</pre>
<p>The function good() returns true if no errors have occurred with
the current stream, false otherwise.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="bad.html">bad</a><br>
<a href="clear.html">clear</a><br>
<a href="eof.html">eof</a><br>
<a href="fail.html">fail</a><br>
<a href="rdstate.html">rdstate</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
ignore
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
istream& ignore( streamsize num=1, int delim=<strong>EOF</strong> );
</pre>
<p>The ignore() function is used with input streams. It reads and
throws away characters until <em>num</em> characters have been read
(where <em>num</em> defaults to 1) or until the character
<em>delim</em> is read (where <em>delim</em> defaults to
<strong>EOF</strong>).</p>
<p>The ignore() function can sometimes be useful when using the
getline() function together with the >> operator. For
example, if you read some input that is followed by a newline using
the >> operator, the newline will remain in the input as the
next thing to be read. Since getline() will by default stop reading
input when it reaches a newline, a subsequent call to getline() will
return an empty string. In this case, the ignore() function could
be called before getline() to "throw away" the newline.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="get.html">get</a><br>
<a href="getline.html">getline</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
open
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
void open( const char *filename );
void open( const char *filename, openmode mode = default_mode );
</pre>
<p>The function open() is used with file streams. It opens
<em>filename</em> and associates it with the current stream. The
optional <a href="../io_flags.html#mode_flags">io stream mode
flag</a> <em>mode</em> defaults to ios::in for ifstream, ios::out for
ofstream, and ios::in|ios::out for fstream.</p>
<p>If open() fails, the resulting stream will evaluate to false when
used in a Boolean expression. For example:</p>
<pre class="prettyprint">
ifstream inputStream;
inputStream.open("file.txt");
if( !inputStream ) {
cerr << "Error opening input stream" << endl;
return;
}
</pre>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="constructors.html">I/O Constructors</a><br>
<a href="close.html">close</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppio/all.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:25:44 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>
<hr>
<div class="name-format">
peek
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <fstream>
int peek();
</pre>
<p>The function peek() is used with input streams, and returns the
next character in the stream or <strong>EOF</strong> if the end of
file is read. peek() does not remove the character from the
stream.</p>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="get.html">get</a><br>
<a href="putback.html">putback</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">