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
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void err(char const *msg)
{
cout << msg << '\n';
}
void err(char const *msg, long value)
{
cout << msg << value << '\n';
}
void read(fstream &index, fstream &strings)
{
int idx;
if (!(cin >> idx)) // read index
return err("line number expected");
index.seekg(idx * sizeof(long)); // go to index-offset
long offset;
if
(
!index.read // read the line-offset
(
reinterpret_cast<char *>(&offset),
sizeof(long)
)
)
return err("no offset for line", idx);
if (!strings.seekg(offset)) // go to the line's offset
return err("can't get string offset ", offset);
string line;
if (!getline(strings, line)) // read the line
return err("no line at ", offset);
cout << "Got line: " << line << '\n'; // show the line
}
void write(fstream &index, fstream &strings)
{
string line;
if (!getline(cin, line)) // read the line
return err("line missing");
strings.seekp(0, ios::end); // to strings
index.seekp(0, ios::end); // to index
long offset = strings.tellp();
if
(
!index.write // write the offset to index
(
reinterpret_cast<char *>(&offset),
sizeof(long)
)
)
return err("Writing failed to index: ", offset);
if (!(strings << line << '\n')) // write the line itself
return err("Writing to `strings' failed");
// confirm writing the line
cout << "Write at offset " << offset << " line: " << line << '\n';
}
int main()
{
fstream index("index", ios::trunc | ios::in | ios::out);
fstream strings("strings", ios::trunc | ios::in | ios::out);
cout << "enter `r <number>' to read line <number> or "
"w <line>' to write a line\n"
"or enter `q' to quit.\n";
while (true)
{
cout << "r <nr>, w <line>, q ? "; // show prompt
index.clear();
strings.clear();
string cmd;
cin >> cmd; // read cmd
if (cmd == "q") // process the cmd.
return 0;
if (cmd == "r")
read(index, strings);
else if (cmd == "w")
write(index, strings);
else
cout << "Unknown command: " << cmd << '\n';
}
}
|