forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
fileio.cpp.html
67 lines (57 loc) · 5.46 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
66
67
<!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>fileio.cpp</title>
</head>
<body bgcolor="white">
<pre><tt><i><font color="#9A1900">// This program shows how C++-based file I/O works.</font></i>
<i><font color="#9A1900">// It will print a file to the screen two times.</font></i>
<i><font color="#9A1900">// included so we can use cout</font></i>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><iostream></font>
<i><font color="#9A1900">// file I/O</font></i>
<b><font color="#000080">#include</font></b> <font color="#FF0000"><fstream></font>
<i><font color="#9A1900">// cstdlib is where exit() lives</font></i>
<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">// 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 one and 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">// ifstream stands for "input file stream"</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>
<i><font color="#9A1900">// if the file wasn't found, output an error message and 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 '"</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">"' for reading"</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 characters one by one, until reading fails (we hit the end of the file)</font></i>
<font color="#009900">char</font> g<font color="#990000">;</font>
<b><font color="#0000FF">while</font></b> <font color="#990000">(</font>file<font color="#990000">.</font><b><font color="#000000">get</font></b><font color="#990000">(</font>g<font color="#990000">))</font> <font color="#FF0000">{</font>
cout <font color="#990000"><<</font> g<font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// a nice pretty separator</font></i>
cout <font color="#990000"><<</font> <font color="#FF0000">"----------------------------------------"</font> <font color="#990000"><<</font> endl<font color="#990000">;</font>
<i><font color="#9A1900">// once we hit the end of the file we are marked as "failed", so clear that</font></i>
<i><font color="#9A1900">// then "seek" to the beginning of the file to start again</font></i>
file<font color="#990000">.</font><b><font color="#000000">clear</font></b><font color="#990000">();</font> <i><font color="#9A1900">// Clears the _state_ of the file, not its contents!</font></i>
file<font color="#990000">.</font><b><font color="#000000">seekg</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<i><font color="#9A1900">// Read the file again, and print to the screen</font></i>
<b><font color="#0000FF">while</font></b> <font color="#990000">(</font>file<font color="#990000">.</font><b><font color="#000000">get</font></b><font color="#990000">(</font>g<font color="#990000">))</font> <font color="#FF0000">{</font>
cout <font color="#990000"><<</font> g<font color="#990000">;</font>
<font color="#FF0000">}</font>
<i><font color="#9A1900">// close the file</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="#990000">;</font>
<font color="#FF0000">}</font>
</tt></pre>
</body>
</html>