Debian Codegraph @

yo/iostreams/examples/readwrite2.cc

raw
1 #include <iostream>
2 #include <fstream>
3 #include <string>
4 using namespace std;
5
6 void err(char const *msg); // see earlier example
7 void err(char const *msg, long value);
8
9 void read(istream &index, istream &strings)
10 {
11 index.clear();
12 strings.clear();
13
14 // insert the body of the read() function of the earlier example
15 }
16
17
18 void write(ostream &index, ostream &strings)
19 {
20 index.clear();
21 strings.clear();
22
23 // insert the body of the write() function of the earlier example
24 }
25
26 int main()
27 {
28 ifstream index_in("index", ios::trunc | ios::in | ios::out);
29 ifstream strings_in("strings", ios::trunc | ios::in | ios::out);
30 ostream index_out(index_in.rdbuf());
31 ostream strings_out(strings_in.rdbuf());
32
33 cout << "enter `r <number>' to read line <number> or "
34 "w <line>' to write a line\n"
35 "or enter `q' to quit.\n";
36
37 while (true)
38 {
39 cout << "r <nr>, w <line>, q ? "; // show prompt
40
41 string cmd;
42
43 cin >> cmd; // read cmd
44
45 if (cmd == "q") // process the cmd.
46 return 0;
47
48 if (cmd == "r")
49 read(index_in, strings_in);
50 else if (cmd == "w")
51 write(index_out, strings_out);
52 else
53 cout << "Unknown command: " << cmd << '\n';
54 }
55 }