Tech Support Guy banner
  • Please post in our Community Feedback thread for help with the new forum software! If you are having trouble logging in, please Contact Us for assistance.
Status
Not open for further replies.
1 - 4 of 4 Posts

·
Registered
Joined
·
45 Posts
Discussion Starter · #1 ·
I missed my C++ class when my instructor was telling us how to open an external file and write the data to another external file.
Using my book, I've learned most of what I have to do, except in the book all the examples define a file in the program that will be written. In the program I'm supposed to write, the user is supposed to type in the name of the file they want created. I must be leaving out some sort of command because when running the version I have so far, the file the user types in gets created, but the new file is empty. Also, I know that the file the user types in to open is being opened successfully. I could get the data to open in the console window, but when rewriting the code to output it in the new file, I just get nothing.(The code below is under construction, so please excuse any sloppiness. I've still got a lot more to write, but I want to get past this problem first.)
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;



//===================================================================================

int main()
{
	ifstream fin;
	ofstream fout;
	string PRollInput, PRollReport;
	double a, b, c;

cout << "Assignment 7D written by xxxxx" << endl << endl;
cout << "Enter name of payroll input file: " << endl;
cin >> PRollInput;
cout << "Enter name of payroll report file: " << endl;
cin >> PRollReport;

	fin.open(PRollInput.c_str() );

	if (fin.fail())
	{
		cout << "Cannot open input file "
	           << PRollInput
			   << ". Aborting program."
			   << endl;
return 0;
	}
	fout.open(PRollReport.c_str());
		if (fout.fail())
			{
				cout << "Cannot open file "
					<< PRollReport
					<< ". Aborting program."
					<< endl;
				return 0;
			}
	
while (fin >> a >> b >> c)
	{
	fout << "ASSIGNMENT 7D WRITTEN BY xxxxx " << endl << endl;
	fout << "PAYROLL REPORT USING DISK INPUT FILE " << PRollInput << endl;
	fout << "EMPLOYEE      WAGE      HOURS      GROSS" << endl;
	fout << "   ID         RATE      WORKED     EARNINGS" << endl;
	    fout << fixed;
		fout << setprecision(0) << setw(9) << a
			 << setw(6) << '$'
			 <<  setprecision(2) << setw(5) << b
			 << setw(5) << ' '
			 << setprecision(2) << setw(6) << c
			 << endl;
	}
	return 0;
			
	
		

	fin.close();
	fout.close();


	return 0;
}
 

·
Registered
Joined
·
307 Posts
In order to test this out, can you include the file that you read from, the 'fin' file in your next post.
 

·
Registered
Joined
·
2,636 Posts
excusing the "under construction" but here's a tip anyways.

Code:
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main() {
    cout << "Assignment 7D written by xxxxx" << endl << endl;
    cout << "Enter name of payroll input file: ";
    string PRollInput = " ";
    getline(cin,PRollInput);
    cout << "Enter name of payroll report file: ";
    string PRollReport = " ";
    getline(cin,PRollReport);
    ifstream fin(PRollInput.c_str());
    if (!fin) {
        cout << endl << "Cannot open input file " << PRollInput 
             << ". Aborting program." << endl;
        exit(EXIT_FAILURE);
    }
    ofstream fout(PRollReport.c_str());
    if (!fout) {
        cout << endl << "Cannot open file " << PRollReport << ". Aborting program."
             << endl;
        exit(EXIT_FAILURE);
    }
    fout << "ASSIGNMENT 7D WRITTEN BY xxxxx " << endl << endl;
    fout << "PAYROLL REPORT USING DISK INPUT FILE " << PRollInput << endl;
    double a = 0.0;
    double b = 0.0;
    double c = 0.0;
    while (fin >> a >> b >> c) {
        fout << "EMPLOYEE      WAGE      HOURS      GROSS" << endl;
        fout << "   ID         RATE      WORKED     EARNINGS" << endl;
        fout << fixed;
        fout << setprecision(0) << setw(9) << a << setw(6) << '$' 
             << setprecision(2) << setw(5) << b << setw(5) << ' '
             << setprecision(2) << setw(6) << c << endl << endl;
    }
}
No need to define an ifstream/ostream and then .open it. You do it all on 1 line. You also do not need to explicitly close the streams. The return for main is not needed. exit() is more desired than return if the file fails to open. It is also good form in c++ to define your variables near where they are first used. It's also good to actually initialize your doubles to 0.0 and your strings to " ". Also, getline() is your friend.

@Regicide

for an input file, just put a number on each line. (for like 15 lines). That will do good enough to test with.
 
1 - 4 of 4 Posts
Status
Not open for further replies.
Top