forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
inlab-skeleton.cpp
71 lines (61 loc) · 2.01 KB
/
inlab-skeleton.cpp
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
// This program is the skeleton code for the lab 10 in-lab.
// It uses C++ streams for the file input,
// and just prints out the data when read in from the file.
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
using namespace std;
// main(): we want to use parameters
int main (int argc, char** argv) {
// verify the correct number of parameters
if (argc != 2) {
cout << "Must supply the input file name as the only parameter" << endl;
exit(1);
}
// attempt to open the supplied file
// must be opened in binary mode as otherwise trailing whitespace is discarded
ifstream file(argv[1], ifstream::binary);
// report any problems opening the file and then exit
if (!file.is_open()) {
cout << "Unable to open file '" << argv[1] << "'." << endl;
exit(2);
}
// read in the first section of the file: the prefix codes
while (true) {
string character, prefix;
// read in the first token on the line
file >> character;
// did we hit the separator?
if (character[0] == '-' && character.length() > 1) {
break;
}
// check for space
if (character == "space") {
character = " ";
}
// read in the prefix code
file >> prefix;
// do something with the prefix code
cout << "character '" << character << "' has prefix code '" << prefix << "'" << endl;
}
// read in the second section of the file: the encoded message
stringstream sstm;
while (true) {
string bits;
// read in the next set of 1's and 0's
file >> bits;
// check for the separator
if (bits[0] == '-') {
break;
}
// add it to the stringstream
sstm << bits;
}
string allbits = sstm.str();
// at this point, all the bits are in the 'allbits' string
cout << "All the bits: " << allbits << endl;
// close the file before exiting
file.close();
return 0
}