forked from uva-cs/pdr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileio.cpp.html
65 lines (56 loc) · 6 KB
/
fileio.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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>fileio.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// This program shows how C++-based file I/O works.</span></i>
<i><span style="color:#9A1900">// It will print a file to the screen two times.</span></i>
<i><span style="color:#9A1900">// included so we can use cout</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><iostream></span>
<i><span style="color:#9A1900">// file I/O</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><fstream></span>
<i><span style="color:#9A1900">// cstdlib is where exit() lives</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000"><cstdlib></span>
<b><span style="color:#0000FF">using</span></b> <b><span style="color:#0000FF">namespace</span></b> std<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// we want to use parameters</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">main</span></b><span style="color:#990000">(</span><span style="color:#009900">int</span> argc<span style="color:#990000">,</span> <span style="color:#009900">char</span><span style="color:#990000">**</span> argv<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// verify the correct number of parameters</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span>argc <span style="color:#990000">!=</span> <span style="color:#993399">2</span><span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Must supply the input file name as the one and only parameter"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">1</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// attempt to open the supplied file</span></i>
<i><span style="color:#9A1900">// ifstream stands for "input file stream"</span></i>
<span style="color:#008080">ifstream</span> <b><span style="color:#000000">file</span></b><span style="color:#990000">(</span>argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]);</span>
<i><span style="color:#9A1900">// if the file wasn't found, output an error message and exit</span></i>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(!</span>file<span style="color:#990000">.</span><b><span style="color:#000000">is_open</span></b><span style="color:#990000">())</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"Unable to open '"</span> <span style="color:#990000"><<</span> argv<span style="color:#990000">[</span><span style="color:#993399">1</span><span style="color:#990000">]</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">"' for reading"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<b><span style="color:#000000">exit</span></b><span style="color:#990000">(</span><span style="color:#993399">2</span><span style="color:#990000">);</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// read in characters one by one, until reading fails (we hit the end of the file)</span></i>
<span style="color:#009900">char</span> g<span style="color:#990000">;</span>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span>file<span style="color:#990000">.</span><b><span style="color:#000000">get</span></b><span style="color:#990000">(</span>g<span style="color:#990000">))</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> g<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// a nice pretty separator</span></i>
cout <span style="color:#990000"><<</span> <span style="color:#FF0000">"----------------------------------------"</span> <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<i><span style="color:#9A1900">// once we hit the end of the file we are marked as "failed", so clear that</span></i>
<i><span style="color:#9A1900">// then "seek" to the beginning of the file to start again</span></i>
file<span style="color:#990000">.</span><b><span style="color:#000000">clear</span></b><span style="color:#990000">();</span> <i><span style="color:#9A1900">// Clears the _state_ of the file, not its contents!</span></i>
file<span style="color:#990000">.</span><b><span style="color:#000000">seekg</span></b><span style="color:#990000">(</span><span style="color:#993399">0</span><span style="color:#990000">);</span>
<i><span style="color:#9A1900">// Read the file again, and print to the screen</span></i>
<b><span style="color:#0000FF">while</span></b> <span style="color:#990000">(</span>file<span style="color:#990000">.</span><b><span style="color:#000000">get</span></b><span style="color:#990000">(</span>g<span style="color:#990000">))</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> g<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// close the file</span></i>
file<span style="color:#990000">.</span><b><span style="color:#000000">close</span></b><span style="color:#990000">();</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>