You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include <cstdlib< // for the fail member function
22
+
#include <fstream< // for file I/O definitions
23
+
#include <iostream< // 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 << "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>
21
41
<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>
22
42
<p>For more on Error Handling, see section 1.11.</p>
<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 <cstdlib> // for the exit function
5
-
#include <fstream> // for I/O member functions
6
-
#include <iostream> // for cout
7
-
using namespace std; // To avoid writing std:: before standard library components
4
+
<blockquote>
5
+
<program language="cpp">
6
+
<input>
7
+
#include <cstdlib> // for the exit function
8
+
#include <fstream> // for I/O member functions
9
+
#include <iostream> // for cout
10
+
using namespace std; // To avoid writing std:: before standard library components
8
11
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
15
18
16
-
// Prompt the user for input and output file names
17
-
cout << "This program will sum three numbers taken from an input\n"
18
-
<< "file and write the sum to an output file." << endl;
19
-
cout << "Enter the input file name (maximum of 15 characters):\n";
20
-
cin >> in_file_name;
21
-
cout << "\nEnter the output file name (maximum of 15 characters):\n";
22
-
cin >> out_file_name;
23
-
cout << endl;
19
+
// Prompt the user for input and output file names
20
+
cout << "This program will sum three numbers taken from an input\n"
21
+
<< "file and write the sum to an output file." << endl;
22
+
cout << "Enter the input file name (maximum of 15 characters):\n";
23
+
cin >> in_file_name;
24
+
cout << "\nEnter the output file name (maximum of 15 characters):\n";
25
+
cin >> out_file_name;
26
+
cout << endl;
24
27
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);
28
31
29
-
if (in_stream.fail() || out_stream.fail()) {
30
-
cout << "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 << "Input or output file opening failed.\n";
34
+
exit(1); // Terminate the program with an error code
35
+
}
33
36
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;
36
39
37
-
// Read the first three numbers from the input file
38
-
cout << "Reading numbers from the file " << in_file_name << endl;
<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>
0 commit comments