I/O files: reading, manipulating and outputting data
I wish to input (read) the contents of the file called "numbers.dat" (that appears at the bottom of this writeup) into the file (sum.dat) following immediately here below; with the view of manipulating these contents and outputting the results within this file called "sum.dat". However, all my previous attempts have ended with: "Input file opening failed"!
I am using Code::Blocks 10.05 running on Windows.
I am unable to figure out why the input file fails to open. What is wrong here?
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
char in_file_name[16], out_file_name[16];
ifstream in_stream;
ofstream out_stream;
cout << "Enter the input file name (maximum of 15 characters):\n";
cin >> in_file_name;
cout << "Enter the output file name (maximum of 15 characters):\n";
cin >> out_file_name;
cout << "I will read numbers from the file "
<< in_file_name << " and\n"
<< "place the sum in the file "
<< out_file_name << endl;
in_stream.open(in_file_name);
if (in_stream.fail ())
{
cout << "Input file opening failed.\n";
exit(1);
out_stream.open(out_file_name);
if (out_stream.fail ())
{
cout << "Output file opening failed.\n";
exit(1);
}
int first, second, third;
in_stream >> first >> second >> third;
out_stream << "The Sum of the first 3\n"
<< "numbers in " << in_file_name << endl
<< " is " << (first + second + third)
<< endl;
in_stream.close();
out_stream.close();
cout << " End of Program.\n";
}
return 0;
}
//Following is the file numbers.dat
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2, num3, num4;
string numbers;
cout << "Enter the four numbers: " << endl;
cin >> num1 >> num2 >> num3 >> num4;
cout << "The numbers are: " << num1 << " "<< num2 << " "
<< num3 << " " << num4;
ofstream writer("numbers.dat");
if(!writer)
{
cout << "Error opening file for output" << endl;
return -1;
}
writer << numbers << endl;
writer.close();
return 0;
}
I/O files: reading, manipulating and outputting data
Thanks CornedBee! I have done everything - including commenting out the string called numbers - as you advised. I did save the second snippet under the name numbers.cpp, before I had it compiled and run successfully with the necessary inputs. However, when I attempted to run the other program in the first snippet it could not open the file "numbers.dat". This other program still says that "Input file opening failed".
Any other ideas on how to overcome this? I appreciate!!
I/O files: reading, manipulating and outputting data
Here below is the updated numbers.cpp program. When I run it, I am prompted to input the required 4 numbers as expected. I did input the 4 numbers. However, after that only a blank numbers.dat is created. Thanks!
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2, num3, num4;
//string numbers;
cout << "Enter the four numbers: " << endl;
cin >> num1 >> num2 >> num3 >> num4;
cout << "The numbers are: " << num1 << " "<< num2 << " " << num3 << " " << num4;
ofstream writer("numbers.dat");
if(!writer)
{
cout << "Error opening file for output" << endl;
return -1;
}
// writer << numbers << endl;
writer.close();
return 0;
}
I/O files: reading, manipulating and outputting data
Thanks CornedBee! One issue is now resolved. The file numbers.dat now contains the numbers input in the program. However, when I run the other program from the first snippet (that I also reproduce below for clarity/ convenience), it is still unable to open numbers.dat and sum up the numbers contained in the file numbers.dat as required of it. I still get the "input file opening failed" message. I reproduce the corrected numbers.cpp program and the other program - that is meant to open the file numbers.dat and sum up its contents - that appeared in the first snippet.
Code:
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int num1, num2, num3, num4;
//string numbers;
cout << "Enter the four numbers: " << endl;
cin >> num1 >> num2 >> num3 >> num4;
cout << "The numbers are: " << num1 << " "<< num2 << " " << num3 << " " << num4;
ofstream writer("numbers.dat");
if(!writer)
{
cout << "Error opening file for output" << endl;
return -1;
}
writer << num1 << num2 << num3 << num4 << endl;
writer.close();
return 0;
}
Next is the other program
Code:
#include <fstream>
#include <iostream>
#include <cstdlib>
int main()
{
using namespace std;
char in_file_name[16], out_file_name[16];
ifstream in_stream;
ofstream out_stream;
cout << "Enter the input file name (maximum of 15 characters):\n";
cin >> in_file_name;
cout << "Enter the output file name (maximum of 15 characters):\n";
cin >> out_file_name;
cout << "I will read numbers from the file "
<< in_file_name << " and\n"
<< "place the sum in the file "
<< out_file_name << endl;
in_stream.open(in_file_name);
if (in_stream.fail ())
{
cout << "Input file opening failed.\n";
exit(1);
out_stream.open(out_file_name);
if (out_stream.fail ())
{
cout << "Output file opening failed.\n";
exit(1);
}
int first, second, third;
in_stream >> first >> second >> third;
out_stream << "The Sum of the first 3\n"
<< "numbers in " << in_file_name << endl
<< " is " << (first + second + third)
<< endl;
in_stream.close();
out_stream.close();
cout << " End of Program.\n";
}
return 0;
}