~ubuntu-branches/ubuntu/trusty/c++-annotations/trusty

« back to all changes in this revision

Viewing changes to yo/iostreams/src/read.yo.OFF

  • Committer: Package Import Robot
  • Author(s): Frank B. Brokken
  • Date: 2011-09-12 16:08:05 UTC
  • mfrom: (1.1.17 upstream)
  • Revision ID: package-import@ubuntu.com-20110912160805-r9dq68beojgzuien
Tags: 9.0.2-1
New upstream release (editorial changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
In order to be able to read from a file an tt(ifstream) object must be
 
2
created, in order to be able to read from a string stream an tt(istrstream)
 
3
object must be created.
 
4
 
 
5
To open a file to read from, the tt(ifstream) constructor receives
 
6
the name of the file to be opened:
 
7
 
 
8
    center(tt(ifstream in("infile");))
 
9
 
 
10
By default this will result in the opening of the file for reading. The file
 
11
must exist for the tt(ifstream) object construction to succeed.
 
12
Instead of the shorthand form to open a file for reading, and explicit tt(ios)
 
13
flag may be used as well:
 
14
 
 
15
    center(tt(ifstream in("infile", ios::in);))
 
16
 
 
17
As with the tt(ofstream) objects, tt(ifstream) objects may be constructed
 
18
first, and opened later:
 
19
        verb(
 
20
    ifstream
 
21
        ifstr;
 
22
 
 
23
    ifstr.open("infile");
 
24
        )
 
25
    Normally, information will be extracted from the tt(ifstream) object using
 
26
the extraction operator rshift(), in the way it is used with the standard stream
 
27
tt(cin), e.g.:
 
28
 
 
29
    center(tt(in >> x >> y;))
 
30
 
 
31
By default, the extraction operator skips blanks: between words, between
 
32
characters, between numbers, etc.. Consequently, if the input consists of the
 
33
following information: verb(
 
34
    12
 
35
    13
 
36
    a b
 
37
    hello world
 
38
)
 
39
then the next code fragment will read tt(12) and tt(13) into tt(x) and tt(y),
 
40
will then return the characters tt(a) and tt(b), and will finally read
 
41
tt(hello) and tt(world) into the character array tt(buffer):
 
42
verb(
 
43
    int
 
44
        x,
 
45
        y;
 
46
    char
 
47
        c,
 
48
        buffer[10];
 
49
 
 
50
    in >> x >> y >> c >> c >> buffer >> buffer;
 
51
)
 
52
Notice that no format specifiers are necessary. The type of the variables
 
53
receiving the extracted information determines the nature of the extraction:
 
54
integer values for tt(int)s, white space delimited strings for tt(char [])s,
 
55
etc..
 
56
 
 
57
Just like the tt(fopen()) function of bf(C) may fail, the construction of the
 
58
tt(ifstream) object might not succeed. When an attempt is made to
 
59
create an tt(ifstream) object, it is a good idea to test the successful
 
60
construction. The tt(ifstream) object returns 0 if its construction failed.
 
61
This value can be used in tests, and the code can throw an exception (see
 
62
section ref(EXCEPTIONS)) or it can handle the failure itself, as in the
 
63
following code:
 
64
 
 
65
verb(
 
66
    #include <iostream>
 
67
    #include <fstream>
 
68
 
 
69
    int main()
 
70
    {
 
71
        ifstream
 
72
            in("");         // creating 'in' fails
 
73
 
 
74
        if (!in)
 
75
        {
 
76
            cerr << "creating ifstream object failed\n";
 
77
            exit(1);
 
78
        }
 
79
    }
 
80
)
 
81
 
 
82
Analogous to an tt(ifstream) object, an tt(istrstream) object can be
 
83
created. Here no filename is required. E.g.,
 
84
        centt(istrstream text("hello world");)
 
85
    opens an tt(istrstream) object that is initialized by an ascii-z string.