Skip to content

Commit 4614d02

Browse files
authored
Merge pull request #243 from moisedk/issue/242
Issue/242
2 parents 53a588a + f91956a commit 4614d02

File tree

3 files changed

+134
-109
lines changed

3 files changed

+134
-109
lines changed

pretext/Input_and_Output/DealingWithIOFailures.ptx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,29 @@
1515
occured, in_stream may be in a corrupted state and it is best not to attempt any more
1616
operations with it.</p>
1717
<p>The following example code fragment safely quits the program entirely in case an I/O operation fails:</p>
18-
<raw format="html" xml:space="preserve">&lt;div&gt;
19-
&lt;iframe height="400px" width="100%" src="https://repl.it/@CodyWMitchell/File-Handling-1?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"&gt;&lt;/iframe&gt;
20-
&lt;/div&gt;</raw>
18+
<blockquote>
19+
<program language="cpp">
20+
<input>
21+
#include &lt;cstdlib&lt; // for the fail member function
22+
#include &lt;fstream&lt; // for file I/O definitions
23+
#include &lt;iostream&lt; // for cout definition
24+
using namespace std;
25+
26+
int main() {
27+
ifstream in_stream;
28+
// Try changing this to realFile.txt
29+
in_stream.open("realFile.txt");
30+
if (in_stream.fail()) {
31+
cout &lt;&lt; "Sorry, the file couldn't be opened!\n";
32+
exit(1); // This exit value indicates an error happened (usual exit
33+
// value is 0)
34+
}
35+
36+
return 0;
37+
}
38+
</input>
39+
</program>
40+
</blockquote>
2141
<p>After opening the <q>myFile.txt</q> file, the <c>if</c> conditional checks to see if there was an error. If so, the program will output the apologetic error message and then exit. The <c>exit(1)</c> function from the library <c>cstdlib</c> enables the program to terminate at that point and have it return a <q>1</q> versus a <q>0</q>, indicating an Error has occurred.</p>
2242
<p>For more on Error Handling, see section 1.11.</p>
2343
</section>
Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,66 @@
11
<section xml:id="io-putting-it-all-together">
22
<title>Putting it all Together</title>
33
<p>The following program combines all of the elements above and asks the user for the input and output filenames. After testing for open failures, it will read three numbers from the input file and write the sum into the output file.</p>
4-
<pre>#include &lt;cstdlib&gt; // for the exit function
5-
#include &lt;fstream&gt; // for I/O member functions
6-
#include &lt;iostream&gt; // for cout
7-
using namespace std; // To avoid writing std:: before standard library components
4+
<blockquote>
5+
<program language="cpp">
6+
<input>
7+
#include &lt;cstdlib&gt; // for the exit function
8+
#include &lt;fstream&gt; // for I/O member functions
9+
#include &lt;iostream&gt; // for cout
10+
using namespace std; // To avoid writing std:: before standard library components
811

9-
int main() {
10-
// Declare variables for file names and file streams
11-
char in_file_name[16], // Arrays to store filenames (max 15 chars + null terminator)
12-
out_file_name[16];
13-
ifstream in_stream; // Input file stream object
14-
ofstream out_stream;// Output file stream object
12+
int main() {
13+
// Declare variables for file names and file streams
14+
char in_file_name[16], // Arrays to store filenames (max 15 chars + null terminator)
15+
out_file_name[16];
16+
ifstream in_stream; // Input file stream object
17+
ofstream out_stream;// Output file stream object
1518

16-
// Prompt the user for input and output file names
17-
cout &lt;&lt; "This program will sum three numbers taken from an input\n"
18-
&lt;&lt; "file and write the sum to an output file." &lt;&lt; endl;
19-
cout &lt;&lt; "Enter the input file name (maximum of 15 characters):\n";
20-
cin &gt;&gt; in_file_name;
21-
cout &lt;&lt; "\nEnter the output file name (maximum of 15 characters):\n";
22-
cin &gt;&gt; out_file_name;
23-
cout &lt;&lt; endl;
19+
// Prompt the user for input and output file names
20+
cout &lt;&lt; "This program will sum three numbers taken from an input\n"
21+
&lt;&lt; "file and write the sum to an output file." &lt;&lt; endl;
22+
cout &lt;&lt; "Enter the input file name (maximum of 15 characters):\n";
23+
cin &gt;&gt; in_file_name;
24+
cout &lt;&lt; "\nEnter the output file name (maximum of 15 characters):\n";
25+
cin &gt;&gt; out_file_name;
26+
cout &lt;&lt; endl;
2427

25-
// Condensed input and output file opening and checking.
26-
in_stream.open(in_file_name);
27-
out_stream.open(out_file_name);
28+
// Condensed input and output file opening and checking.
29+
in_stream.open(in_file_name);
30+
out_stream.open(out_file_name);
2831

29-
if (in_stream.fail() || out_stream.fail()) {
30-
cout &lt;&lt; "Input or output file opening failed.\n";
31-
exit(1); // Terminate the program with an error code
32-
}
32+
if (in_stream.fail() || out_stream.fail()) {
33+
cout &lt;&lt; "Input or output file opening failed.\n";
34+
exit(1); // Terminate the program with an error code
35+
}
3336

34-
// Declare variables to store numbers and their sum
35-
double firstn, secondn, thirdn, sum = 0.0;
37+
// Declare variables to store numbers and their sum
38+
double firstn, secondn, thirdn, sum = 0.0;
3639

37-
// Read the first three numbers from the input file
38-
cout &lt;&lt; "Reading numbers from the file " &lt;&lt; in_file_name &lt;&lt; endl;
39-
in_stream &gt;&gt; firstn &gt;&gt; secondn &gt;&gt; thirdn;
40-
sum = firstn + secondn + thirdn;
40+
// Read the first three numbers from the input file
41+
cout &lt;&lt; "Reading numbers from the file " &lt;&lt; in_file_name &lt;&lt; endl;
42+
in_stream &gt;&gt; firstn &gt;&gt; secondn &gt;&gt; thirdn;
43+
sum = firstn + secondn + thirdn;
4144

42-
// The following set of lines will write to the screen
43-
cout &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name &lt;&lt; " is "
44-
&lt;&lt; sum &lt;&lt; endl;
45+
// The following set of lines will write to the screen
46+
cout &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name &lt;&lt; " is "
47+
&lt;&lt; sum &lt;&lt; endl;
4548

46-
cout &lt;&lt; "Placing the sum into the file " &lt;&lt; out_file_name &lt;&lt; endl;
49+
cout &lt;&lt; "Placing the sum into the file " &lt;&lt; out_file_name &lt;&lt; endl;
4750

48-
// The following set of lines will write to the output file
49-
out_stream &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name
50-
&lt;&lt; " is " &lt;&lt; sum &lt;&lt; endl;
51+
// The following set of lines will write to the output file
52+
out_stream &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name
53+
&lt;&lt; " is " &lt;&lt; sum &lt;&lt; endl;
5154

52-
// Close the file streams
53-
in_stream.close();
54-
out_stream.close();
55+
// Close the file streams
56+
in_stream.close();
57+
out_stream.close();
5558

56-
cout &lt;&lt; "End of Program." &lt;&lt; endl;
59+
cout &lt;&lt; "End of Program." &lt;&lt; endl;
5760

58-
return 0;
59-
}</pre>
60-
<raw format="html" xml:space="preserve">&lt;div&gt;
61-
&lt;iframe height="400px" width="100%" src="https://repl.it/@CodyWMitchell/File-Handling-3?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"&gt;&lt;/iframe&gt;
62-
&lt;/div&gt;</raw>
61+
return 0;
62+
</input>
63+
</program>
64+
}</blockquote>
6365
</section>
6466

pretext/Input_and_Output/TheEOF.ptx

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,67 +26,72 @@
2626
<p>Here is an example of a program that essentially uses the second technique
2727
mentioned above to read all the numbers in a file and output them in a neater format.
2828
The <c>while</c> loop to scan through a file is located in the <c>make_neat(...)</c> function.</p>
29-
<pre>// Illustrates output formatting instructions.
30-
// Read all the numbers in the file rawdata.dat and write the numbers
31-
// to the screen and to the file neat.dat in a neatly formatted way.
29+
<blockquote>
30+
<program language="cpp">
31+
<input>
32+
// Illustrates output formatting instructions.
33+
// Read all the numbers in the file rawdata.dat and write the numbers
34+
// to the screen and to the file neat.dat in a neatly formatted way.
3235

33-
#include &lt;cstdlib&gt; // for the exit function
34-
#include &lt;fstream&gt; // for I/O member functions
35-
#include &lt;iomanip&gt; // for the setw function
36-
#include &lt;iostream&gt; // for cout
37-
using namespace std;
38-
void make_neat(
39-
ifstream &amp;messy_file,
40-
ofstream &amp;neat_file,
41-
int number_after_decimalpoint,
42-
int field_width);
36+
#include &lt;cstdlib&gt; // for the exit function
37+
#include &lt;fstream&gt; // for I/O member functions
38+
#include &lt;iomanip&gt; // for the setw function
39+
#include &lt;iostream&gt; // for cout
40+
using namespace std;
41+
void make_neat(
42+
ifstream &amp;messy_file,
43+
ofstream &amp;neat_file,
44+
int number_after_decimalpoint,
45+
int field_width);
4346

44-
int main() {
45-
ifstream fin;
46-
ofstream fout;
47+
int main() {
48+
ifstream fin;
49+
ofstream fout;
4750

48-
fin.open("rawdata.txt");
49-
if (fin.fail()) { // oops the file did not exist for reading?
50-
cout &lt;&lt; "Input file opening failed." &lt;&lt; endl;
51-
exit(1);
52-
}
51+
fin.open("rawdata.txt");
52+
if (fin.fail()) { // oops the file did not exist for reading?
53+
cout &lt;&lt; "Input file opening failed." &lt;&lt; endl;
54+
exit(1);
55+
}
5356

54-
fout.open("neat.txt");
55-
if (fout.fail()) { // oops the output file open failed!
56-
cout &lt;&lt; "Output file opening failed.\n";
57-
exit(1);
58-
}
59-
make_neat(fin, fout, 5, 12);
57+
fout.open("neat.txt");
58+
if (fout.fail()) { // oops the output file open failed!
59+
cout &lt;&lt; "Output file opening failed.\n";
60+
exit(1);
61+
}
62+
make_neat(fin, fout, 5, 12);
6063

61-
fin.close();
62-
fout.close();
63-
cout &lt;&lt; "End of program." &lt;&lt; endl;
64-
return 0;
65-
}
66-
// Uses iostreams, streams to the screen, and iomanip:
67-
void make_neat(
68-
ifstream &amp;messy_file,
69-
ofstream &amp;neat_file,
70-
int number_after_decimalpoint,
71-
int field_width) {
72-
// set the format for the neater output file.
73-
neat_file.setf(ios::fixed);
74-
neat_file.setf(ios::showpoint);
75-
neat_file.setf(ios::showpos);
76-
neat_file.precision(number_after_decimalpoint);
77-
// set the format for the output to the screen too.
78-
cout.setf(ios::fixed);
79-
cout.setf(ios::showpoint);
80-
cout.setf(ios::showpos);
81-
cout.precision(number_after_decimalpoint);
82-
double next;
83-
while (messy_file &gt;&gt; next) { // while there is still stuff to read
84-
cout &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
85-
neat_file &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
86-
}
87-
}
88-
89-
// Code by Jan Pearce</pre>
64+
fin.close();
65+
fout.close();
66+
cout &lt;&lt; "End of program." &lt;&lt; endl;
67+
return 0;
68+
}
69+
// Uses iostreams, streams to the screen, and iomanip:
70+
void make_neat(
71+
ifstream &amp;messy_file,
72+
ofstream &amp;neat_file,
73+
int number_after_decimalpoint,
74+
int field_width) {
75+
// set the format for the neater output file.
76+
neat_file.setf(ios::fixed);
77+
neat_file.setf(ios::showpoint);
78+
neat_file.setf(ios::showpos);
79+
neat_file.precision(number_after_decimalpoint);
80+
// set the format for the output to the screen too.
81+
cout.setf(ios::fixed);
82+
cout.setf(ios::showpoint);
83+
cout.setf(ios::showpos);
84+
cout.precision(number_after_decimalpoint);
85+
double next;
86+
while (messy_file &gt;&gt; next) { // while there is still stuff to read
87+
cout &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
88+
neat_file &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
89+
}
90+
}
91+
// Code by Jan Pearce
92+
</input>
93+
</program>
94+
</blockquote>
9095
<p>This is the <c>rawdata.txt</c> inputed into the <c>make_neat(...)</c>.</p>
9196
<pre>10 -20 30 -40
9297
500 300 -100 1000
@@ -109,9 +114,7 @@ void make_neat(
109114
-20.00000
110115
+30.00000
111116
-40.00000</pre>
112-
<raw format="html" xml:space="preserve">&lt;div&gt;
113-
&lt;iframe height="400px" width="100%" src="https://repl.it/@CodyWMitchell/File-Handling-2?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"&gt;&lt;/iframe&gt;
114-
&lt;/div&gt;</raw>
117+
115118
<p>The input file <c>rawdata.txt</c> must be in the same directory (folder) as the program in order for it to open successfully. The program will create a file called <q>neat.dat</q> to output the results.</p>
116119
<reading-questions xml:id="rqs-the-eof">
117120
<title>Reading Question</title>

0 commit comments

Comments
 (0)