Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions pretext/Input_and_Output/DealingWithIOFailures.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@
occured, in_stream may be in a corrupted state and it is best not to attempt any more
operations with it.</p>
<p>The following example code fragment safely quits the program entirely in case an I/O operation fails:</p>
<raw format="html" xml:space="preserve">&lt;div&gt;
&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;
&lt;/div&gt;</raw>
<blockquote>
<program language="cpp">
<input>
#include &lt;cstdlib&lt; // for the fail member function
#include &lt;fstream&lt; // for file I/O definitions
#include &lt;iostream&lt; // for cout definition
using namespace std;

int main() {
ifstream in_stream;
// Try changing this to realFile.txt
in_stream.open("realFile.txt");
if (in_stream.fail()) {
cout &lt;&lt; "Sorry, the file couldn't be opened!\n";
exit(1); // This exit value indicates an error happened (usual exit
// value is 0)
}

return 0;
}
</input>
</program>
</blockquote>
<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>
<p>For more on Error Handling, see section 1.11.</p>
</section>
Expand Down
96 changes: 49 additions & 47 deletions pretext/Input_and_Output/PuttingItAllTogether.ptx
Original file line number Diff line number Diff line change
@@ -1,64 +1,66 @@
<section xml:id="io-putting-it-all-together">
<title>Putting it all Together</title>
<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>
<pre>#include &lt;cstdlib&gt; // for the exit function
#include &lt;fstream&gt; // for I/O member functions
#include &lt;iostream&gt; // for cout
using namespace std; // To avoid writing std:: before standard library components
<blockquote>
<program language="cpp">
<input>
#include &lt;cstdlib&gt; // for the exit function
#include &lt;fstream&gt; // for I/O member functions
#include &lt;iostream&gt; // for cout
using namespace std; // To avoid writing std:: before standard library components

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

// Prompt the user for input and output file names
cout &lt;&lt; "This program will sum three numbers taken from an input\n"
&lt;&lt; "file and write the sum to an output file." &lt;&lt; endl;
cout &lt;&lt; "Enter the input file name (maximum of 15 characters):\n";
cin &gt;&gt; in_file_name;
cout &lt;&lt; "\nEnter the output file name (maximum of 15 characters):\n";
cin &gt;&gt; out_file_name;
cout &lt;&lt; endl;
// Prompt the user for input and output file names
cout &lt;&lt; "This program will sum three numbers taken from an input\n"
&lt;&lt; "file and write the sum to an output file." &lt;&lt; endl;
cout &lt;&lt; "Enter the input file name (maximum of 15 characters):\n";
cin &gt;&gt; in_file_name;
cout &lt;&lt; "\nEnter the output file name (maximum of 15 characters):\n";
cin &gt;&gt; out_file_name;
cout &lt;&lt; endl;

// Condensed input and output file opening and checking.
in_stream.open(in_file_name);
out_stream.open(out_file_name);
// Condensed input and output file opening and checking.
in_stream.open(in_file_name);
out_stream.open(out_file_name);

if (in_stream.fail() || out_stream.fail()) {
cout &lt;&lt; "Input or output file opening failed.\n";
exit(1); // Terminate the program with an error code
}
if (in_stream.fail() || out_stream.fail()) {
cout &lt;&lt; "Input or output file opening failed.\n";
exit(1); // Terminate the program with an error code
}

// Declare variables to store numbers and their sum
double firstn, secondn, thirdn, sum = 0.0;
// Declare variables to store numbers and their sum
double firstn, secondn, thirdn, sum = 0.0;

// Read the first three numbers from the input file
cout &lt;&lt; "Reading numbers from the file " &lt;&lt; in_file_name &lt;&lt; endl;
in_stream &gt;&gt; firstn &gt;&gt; secondn &gt;&gt; thirdn;
sum = firstn + secondn + thirdn;
// Read the first three numbers from the input file
cout &lt;&lt; "Reading numbers from the file " &lt;&lt; in_file_name &lt;&lt; endl;
in_stream &gt;&gt; firstn &gt;&gt; secondn &gt;&gt; thirdn;
sum = firstn + secondn + thirdn;

// The following set of lines will write to the screen
cout &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name &lt;&lt; " is "
&lt;&lt; sum &lt;&lt; endl;
// The following set of lines will write to the screen
cout &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name &lt;&lt; " is "
&lt;&lt; sum &lt;&lt; endl;

cout &lt;&lt; "Placing the sum into the file " &lt;&lt; out_file_name &lt;&lt; endl;
cout &lt;&lt; "Placing the sum into the file " &lt;&lt; out_file_name &lt;&lt; endl;

// The following set of lines will write to the output file
out_stream &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name
&lt;&lt; " is " &lt;&lt; sum &lt;&lt; endl;
// The following set of lines will write to the output file
out_stream &lt;&lt; "The sum of the first 3 numbers from " &lt;&lt; in_file_name
&lt;&lt; " is " &lt;&lt; sum &lt;&lt; endl;

// Close the file streams
in_stream.close();
out_stream.close();
// Close the file streams
in_stream.close();
out_stream.close();

cout &lt;&lt; "End of Program." &lt;&lt; endl;
cout &lt;&lt; "End of Program." &lt;&lt; endl;

return 0;
}</pre>
<raw format="html" xml:space="preserve">&lt;div&gt;
&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;
&lt;/div&gt;</raw>
return 0;
</input>
</program>
}</blockquote>
</section>

121 changes: 62 additions & 59 deletions pretext/Input_and_Output/TheEOF.ptx
Original file line number Diff line number Diff line change
Expand Up @@ -26,67 +26,72 @@
<p>Here is an example of a program that essentially uses the second technique
mentioned above to read all the numbers in a file and output them in a neater format.
The <c>while</c> loop to scan through a file is located in the <c>make_neat(...)</c> function.</p>
<pre>// Illustrates output formatting instructions.
// Read all the numbers in the file rawdata.dat and write the numbers
// to the screen and to the file neat.dat in a neatly formatted way.
<blockquote>
<program language="cpp">
<input>
// Illustrates output formatting instructions.
// Read all the numbers in the file rawdata.dat and write the numbers
// to the screen and to the file neat.dat in a neatly formatted way.

#include &lt;cstdlib&gt; // for the exit function
#include &lt;fstream&gt; // for I/O member functions
#include &lt;iomanip&gt; // for the setw function
#include &lt;iostream&gt; // for cout
using namespace std;
void make_neat(
ifstream &amp;messy_file,
ofstream &amp;neat_file,
int number_after_decimalpoint,
int field_width);
#include &lt;cstdlib&gt; // for the exit function
#include &lt;fstream&gt; // for I/O member functions
#include &lt;iomanip&gt; // for the setw function
#include &lt;iostream&gt; // for cout
using namespace std;
void make_neat(
ifstream &amp;messy_file,
ofstream &amp;neat_file,
int number_after_decimalpoint,
int field_width);

int main() {
ifstream fin;
ofstream fout;
int main() {
ifstream fin;
ofstream fout;

fin.open("rawdata.txt");
if (fin.fail()) { // oops the file did not exist for reading?
cout &lt;&lt; "Input file opening failed." &lt;&lt; endl;
exit(1);
}
fin.open("rawdata.txt");
if (fin.fail()) { // oops the file did not exist for reading?
cout &lt;&lt; "Input file opening failed." &lt;&lt; endl;
exit(1);
}

fout.open("neat.txt");
if (fout.fail()) { // oops the output file open failed!
cout &lt;&lt; "Output file opening failed.\n";
exit(1);
}
make_neat(fin, fout, 5, 12);
fout.open("neat.txt");
if (fout.fail()) { // oops the output file open failed!
cout &lt;&lt; "Output file opening failed.\n";
exit(1);
}
make_neat(fin, fout, 5, 12);

fin.close();
fout.close();
cout &lt;&lt; "End of program." &lt;&lt; endl;
return 0;
}
// Uses iostreams, streams to the screen, and iomanip:
void make_neat(
ifstream &amp;messy_file,
ofstream &amp;neat_file,
int number_after_decimalpoint,
int field_width) {
// set the format for the neater output file.
neat_file.setf(ios::fixed);
neat_file.setf(ios::showpoint);
neat_file.setf(ios::showpos);
neat_file.precision(number_after_decimalpoint);
// set the format for the output to the screen too.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(number_after_decimalpoint);
double next;
while (messy_file &gt;&gt; next) { // while there is still stuff to read
cout &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
neat_file &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
}
}

// Code by Jan Pearce</pre>
fin.close();
fout.close();
cout &lt;&lt; "End of program." &lt;&lt; endl;
return 0;
}
// Uses iostreams, streams to the screen, and iomanip:
void make_neat(
ifstream &amp;messy_file,
ofstream &amp;neat_file,
int number_after_decimalpoint,
int field_width) {
// set the format for the neater output file.
neat_file.setf(ios::fixed);
neat_file.setf(ios::showpoint);
neat_file.setf(ios::showpos);
neat_file.precision(number_after_decimalpoint);
// set the format for the output to the screen too.
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(number_after_decimalpoint);
double next;
while (messy_file &gt;&gt; next) { // while there is still stuff to read
cout &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
neat_file &lt;&lt; setw(field_width) &lt;&lt; next &lt;&lt; endl;
}
}
// Code by Jan Pearce
</input>
</program>
</blockquote>
<p>This is the <c>rawdata.txt</c> inputed into the <c>make_neat(...)</c>.</p>
<pre>10 -20 30 -40
500 300 -100 1000
Expand All @@ -109,9 +114,7 @@ void make_neat(
-20.00000
+30.00000
-40.00000</pre>
<raw format="html" xml:space="preserve">&lt;div&gt;
&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;
&lt;/div&gt;</raw>

<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>
<reading-questions xml:id="rqs-the-eof">
<title>Reading Question</title>
Expand Down