File: copying.yo

package info (click to toggle)
c%2B%2B-annotations 12.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 13,044 kB
  • sloc: cpp: 24,337; makefile: 1,517; ansic: 165; sh: 121; perl: 90
file content (42 lines) | stat: -rw-r--r-- 2,181 bytes parent folder | download | duplicates (4)
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
Usually, files are copied hi(file: copying) either by reading a source file
character by character or line by line. The basic em(mold) to process streams
 hi(stream: processing) is as follows:
        itemization(
            it() Continuous loop:
            enumeration(
                eit() read from the stream
                eit() if reading did not succeed (i.e., tt(fail) returns
                        tt(true)), tt(break) from the loop
                eit() process the information that was read
            )
        )
    Note that reading must em(precede) testing, as
it is only possible to know after actually attempting to read from a file
whether the reading succeeded or not. Of course, variations are possible:
tt(getline(istream &, string &)) (see section ref(ISTREAMREAD)) returns an
tt(istream &), so here reading and testing may be contracted using one
expression. Nevertheless, the above mold represents the general case. So,
the following program may be used to copy tt(cin) to tt(cout):
        verbinclude(-a examples/copycincout.cc)

    Contraction is possible here by combining tt(get) with the
tt(if)-statement, resulting in:
        verb(    if (!cin.get(c))
        break;)

Even so, this would still follow the basic rule:
 `i(read first, test later)'.

    Simply copying a file isn't required very often. More often a situation is
encountered where a file is processed up to a certain point, followed by plain
copying the file's remaining information. The next program illustrates
this. Using tt(ignore) to skip the first line (for the sake of the example it
is assumed that the first line is at most 80 characters long), the second
statement uses yet another overloaded version of the lshift()-operator, in
which a ti(streambuf) pointer hi(inserting streambuf *) is inserted into
a stream. As the member tt(rdbuf) returns a stream's tt(streambuf *), we have
a simple means of inserting a stream's content into an tt(ostream):
        verbinclude(-a examples/copystreambuf.cc)
    This way of copying streams only assumes the existence of a tt(streambuf)
object. Consequently it can be used with all specializations of the
tt(streambuf) class.