forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
inlab-skeleton.cpp.html
85 lines (75 loc) · 7.86 KB
/
inlab-skeleton.cpp.html
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
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>inlab-skeleton.cpp</title>
</head>
<body bgcolor="white">
<pre><tt><i><font color="#9A1900">// This program is the skeleton code for the lab 10 in-lab.</font></i>
<i><font color="#9A1900">// It uses C++ streams for the file input,</font></i>
<i><font color="#9A1900">// and just prints out the data when read in from the file.</font></i>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><iostream></font>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><fstream></font>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><sstream></font>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><cstdlib></font>
<b><font color="#0000FF">using</font></b> <b><font color="#0000FF">namespace</font></b> std<font color="#990000">;</font>
<i><font color="#9A1900">// main(): we want to use parameters</font></i>
<font color="#009900">int</font> <b><font color="#000000">main</font></b> <font color="#990000">(</font><font color="#009900">int</font> argc<font color="#990000">,</font> <font color="#009900">char</font><font color="#990000">**</font> argv<font color="#990000">)</font> <font color="#FF0000">{</font>
<i><font color="#9A1900">// verify the correct number of parameters</font></i>
<b><font color="#0000FF">if</font></b> <font color="#990000">(</font>argc <font color="#990000">!=</font> <font color="#993399">2</font><font color="#990000">)</font> <font color="#FF0000">{</font>
cout <font color="#990000"><<</font> <font color="#FF0000">"Must supply the input file name as the only parameter"</font> <font color="#990000"><<</font> endl<font color="#990000">;</font>
<b><font color="#000000">exit</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// attempt to open the supplied file</font></i>
<i><font color="#9A1900">// must be opened in binary mode as otherwise trailing whitespace is discarded</font></i>
<font color="#008080">ifstream</font> <b><font color="#000000">file</font></b><font color="#990000">(</font>argv<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">],</font> ifstream<font color="#990000">::</font>binary<font color="#990000">);</font>
<i><font color="#9A1900">// report any problems opening the file and then exit</font></i>
<b><font color="#0000FF">if</font></b> <font color="#990000">(!</font>file<font color="#990000">.</font><b><font color="#000000">is_open</font></b><font color="#990000">())</font> <font color="#FF0000">{</font>
cout <font color="#990000"><<</font> <font color="#FF0000">"Unable to open file '"</font> <font color="#990000"><<</font> argv<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">]</font> <font color="#990000"><<</font> <font color="#FF0000">"'."</font> <font color="#990000"><<</font> endl<font color="#990000">;</font>
<b><font color="#000000">exit</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// read in the first section of the file: the prefix codes</font></i>
<b><font color="#0000FF">while</font></b> <font color="#990000">(</font><b><font color="#0000FF">true</font></b><font color="#990000">)</font> <font color="#FF0000">{</font>
<font color="#008080">string</font> character<font color="#990000">,</font> prefix<font color="#990000">;</font>
<i><font color="#9A1900">// read in the first token on the line</font></i>
file <font color="#990000">>></font> character<font color="#990000">;</font>
<i><font color="#9A1900">// did we hit the separator?</font></i>
<b><font color="#0000FF">if</font></b> <font color="#990000">(</font>character<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">]</font> <font color="#990000">==</font> <font color="#FF0000">'-'</font> <font color="#990000">&&</font> character<font color="#990000">.</font><b><font color="#000000">length</font></b><font color="#990000">()</font> <font color="#990000">></font> <font color="#993399">1</font><font color="#990000">)</font> <font color="#FF0000">{</font>
<b><font color="#0000FF">break</font></b><font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// check for space</font></i>
<b><font color="#0000FF">if</font></b> <font color="#990000">(</font>character <font color="#990000">==</font> <font color="#FF0000">"space"</font><font color="#990000">)</font> <font color="#FF0000">{</font>
character <font color="#990000">=</font> <font color="#FF0000">" "</font><font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// read in the prefix code</font></i>
file <font color="#990000">>></font> prefix<font color="#990000">;</font>
<i><font color="#9A1900">// do something with the prefix code</font></i>
cout <font color="#990000"><<</font> <font color="#FF0000">"character '"</font> <font color="#990000"><<</font> character <font color="#990000"><<</font> <font color="#FF0000">"' has prefix code '"</font> <font color="#990000"><<</font> prefix <font color="#990000"><<</font> <font color="#FF0000">"'"</font> <font color="#990000"><<</font> endl<font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// read in the second section of the file: the encoded message</font></i>
<font color="#008080">stringstream</font> sstm<font color="#990000">;</font>
<b><font color="#0000FF">while</font></b> <font color="#990000">(</font><b><font color="#0000FF">true</font></b><font color="#990000">)</font> <font color="#FF0000">{</font>
<font color="#008080">string</font> bits<font color="#990000">;</font>
<i><font color="#9A1900">// read in the next set of 1's and 0's</font></i>
file <font color="#990000">>></font> bits<font color="#990000">;</font>
<i><font color="#9A1900">// check for the separator</font></i>
<b><font color="#0000FF">if</font></b> <font color="#990000">(</font>bits<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">]</font> <font color="#990000">==</font> <font color="#FF0000">'-'</font><font color="#990000">)</font> <font color="#FF0000">{</font>
<b><font color="#0000FF">break</font></b><font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// add it to the stringstream</font></i>
sstm <font color="#990000"><<</font> bits<font color="#990000">;</font>
<font color="#FF0000">}</font>
<font color="#008080">string</font> allbits <font color="#990000">=</font> sstm<font color="#990000">.</font><b><font color="#000000">str</font></b><font color="#990000">();</font>
<i><font color="#9A1900">// at this point, all the bits are in the 'allbits' string</font></i>
cout <font color="#990000"><<</font> <font color="#FF0000">"All the bits: "</font> <font color="#990000"><<</font> allbits <font color="#990000"><<</font> endl<font color="#990000">;</font>
<i><font color="#9A1900">// close the file before exiting</font></i>
file<font color="#990000">.</font><b><font color="#000000">close</font></b><font color="#990000">();</font>
<b><font color="#0000FF">return</font></b> <font color="#993399">0</font>
<font color="#FF0000">}</font>
</tt></pre>
</body>
</html>