Filestream仅将最后一个输入写入文件

时间:2017-02-27 18:15:01

标签: c++ fstream ifstream ofstream

我应该能够从文件流中读取“call_data.txt通过我的代码运行它并将其写入文件流”weekly_call_info.txt“但它只打印输入数据文件”call_data“的最后一行。 TXT“

  #include <iostream>
#include <string>
#include <fstream> 
using namespace std;


class call_record
{
public:
    string cell_number;
    int relays;
    int call_length;
    double net_cost;
    double tax_rate;
    double call_tax;
    double total_cost;
};




    void Input(ifstream &, call_record &);
    void Output(const call_record &);
    void Process(call_record &);



    void Input(ifstream & in, call_record & customer_record)
    {


        in >> customer_record.cell_number;
        in >> customer_record.relays;
        in >> customer_record.call_length;

    }



    void Output(const call_record & customer_record)
    {

        cout.setf(ios::showpoint);
        cout.precision(2);
        cout.setf(ios::fixed);

        ofstream out;
        out.open("weekly_call_info.txt");

        cout << customer_record.cell_number << "\t"
            << customer_record.relays << "\t"
            << customer_record.call_length << "\t"
            << customer_record.net_cost << "\t"
            << customer_record.tax_rate << "\t"
            << customer_record.call_tax << "\t"
            << customer_record.total_cost << endl;



        out << endl << customer_record.cell_number << "\t"
            << customer_record.relays << "\t"
            << customer_record.call_length << "\t"
            << customer_record.net_cost << "\t"
            << customer_record.tax_rate << "\t"
            << customer_record.call_tax << "\t"
            << customer_record.total_cost << endl;





    }

    void Process(call_record & customer_record)
    {
        customer_record.net_cost = customer_record.relays / 50.0 * 0.40 * customer_record.call_length;


        if (1 <= customer_record.relays && customer_record.relays <= 5) {
            customer_record.tax_rate = .01;
        }
        else if (6 <= customer_record.relays && customer_record.relays <= 11) {
            customer_record.tax_rate = .03;
        }
        else if (12 <= customer_record.relays && customer_record.relays <= 20) {
            customer_record.tax_rate = .05;
        }
        else if (21 <= customer_record.relays && customer_record.relays <= 50) {
            customer_record.tax_rate = .08;
        }
        else if (customer_record.relays > 50) {
            customer_record.tax_rate = .12;
        }
        else if (customer_record.relays == 0) {
            customer_record.tax_rate = .01;
        };

        customer_record.call_tax = customer_record.net_cost * customer_record.tax_rate;
        customer_record.total_cost = customer_record.net_cost + customer_record.call_tax;

    }


int main()
{

    call_record customer_record;

    ifstream in;   
    in.open("call_data.txt");

    ofstream out;
    out.open("weekly_call_info.txt");



    if (in.fail())
    {
        cout << "Input file did not open correctly" << endl;
    }
    else
    {
        while (!in.eof())
        {
        Input(in, customer_record);
        Process(customer_record);
        Output(customer_record);
        }
    }

    in.close();
    out.close();

    return 0;
}

0 个答案:

没有答案