Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;ofstream仅从ifstream输出最后一行数据_C++_File_Fstream_Ifstream_Ofstream - Fatal编程技术网

C++ C++;ofstream仅从ifstream输出最后一行数据

C++ C++;ofstream仅从ifstream输出最后一行数据,c++,file,fstream,ifstream,ofstream,C++,File,Fstream,Ifstream,Ofstream,我试图从一个文件中读取数据并将数据输出到一个单独的文件中。唯一的问题是,当我运行程序时,文件上的输出来自输入文件最后一行的数据。我也不想附加输出文件,因为我不想在重新运行程序时重复数据,这就是为什么我不在输出中使用ios::app的原因 #include <iostream> #include <fstream> #include <string> using namespace std; //class declaration class call_rec

我试图从一个文件中读取数据并将数据输出到一个单独的文件中。唯一的问题是,当我运行程序时,文件上的输出来自输入文件最后一行的数据。我也不想附加输出文件,因为我不想在重新运行程序时重复数据,这就是为什么我不在输出中使用ios::app的原因

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

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

};

//Prototypes

void input(ifstream & in, call_record &);
void process(call_record &);
void output(call_record &);

//Input function

void input(ifstream & in, call_record & customer_record){
    in >> customer_record.cell_number >> customer_record.relays >> 
    customer_record.call_length;
}

//Process function
void process(call_record & customer_record){

    if(customer_record.relays >= 0 && customer_record.relays <=5){
        customer_record.tax_rate = 0.01;
    } else if(customer_record.relays >= 6 && customer_record.relays <=11){
        customer_record.tax_rate = 0.03;
    } else if(customer_record.relays >= 12 && customer_record.relays <=20){
        customer_record.tax_rate = 0.05;
    } else if(customer_record.relays >= 21 && customer_record.relays <=50){
        customer_record.tax_rate = 0.12;
    }

    //net cost of call
    customer_record.net_cost = (customer_record.relays/50 * 0.40 * 
customer_record.call_length);

    //cost of tax on call
    customer_record.call_tax = customer_record.net_cost * 
customer_record.tax_rate;

//Total cost of call
customer_record.total_cost = customer_record.net_cost + 
customer_record.call_tax;

}

void output(call_record & customer_record){
ofstream out("weekly_call_info.txt");

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

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

out.close();
}

int main(){

call_record customer_record;

ifstream in("call_data.txt");


if(in.fail()){
    cout << "Your input file does not exist or did not open properly." << 
endl;

} else {
    while (!in.eof()){
        input(in, customer_record);
        process(customer_record);
        output(customer_record);
    }
}

in.close();

return 0;
}
#包括
#包括
#包括
使用名称空间std;
//类声明
课堂通话记录{
公众:
字符串单元号;
int继电器;
int调用长度;
双倍净成本;
双重税率;
双重征税;
总成本加倍;
};
//原型
无效输入(ifstream&in,call_record&);
无效流程(调用记录&);
无效输出(调用记录&);
//输入函数
无效输入(ifstream&in、呼叫记录和客户记录){
在>>客户记录.cell\u编号>>客户记录.relay>>
客户\记录。通话\长度;
}
//过程函数
作废流程(电话记录和客户记录){

如果(customer_record.relays>=0&&customer_record.relays=6&&customer_record.relays=12&&customer_record.relays=21&&customer_record.relays则问题在于每次输出时都要创建
每周通话信息.txt
)调用并截断现有文件。这是因为您没有指定
std::ios\u base::app
(“append”)打开模式标志,因此
每周呼叫信息.txt
的现有内容将丢失。有关显示不同打开模式标志组合的等效C样式模式字符串的有用表格,请参阅本页:

我建议将对std::ostream的引用传递给output()函数,以便将数据输出到该函数:

void output(call_record & customer_record, std::ostream& out){
    out << customer_record.cell_number << "   " << customer_record.relays << "   " 
    << customer_record.call_length << "   " << customer_record.net_cost << "   " 
    << customer_record.tax_rate << "   " << customer_record.call_tax << "   " << 
    customer_record.total_cost << endl;
}

int main(){
    call_record customer_record;

    ifstream in("call_data.txt");


    if(in.fail()){
        cout << "Your input file does not exist or did not open properly." << endl;
    } else {
        ofstream out("weekly_call_info.txt");
        out.setf(ios::showpoint);
        out.precision(2);
        out.setf(ios::fixed);

        for (;;) {
            input(in, customer_record);
            if (!in) break;
            process(customer_record);
            output(customer_record, out);
        }
    }

    return 0;
}
void输出(调用记录和客户记录,std::ostream&out){

找出问题在哪里复制了表单以便我能找到它您代码中的错误是:
while(!in.eof()){
链接解释了这是一个错误的原因。我通读了它,但我仍然不知道如何修复while循环。您能帮我找出它吗。我尝试将while循环更改为while(!in.fail()){但它仍然不起作用。@drescherjmMake输入返回bool。然后在输入中:
返回in>>customer\u record.cell\u number>>customer\u record.relay>>customer\u record.call\u length;
然后将
while(!in.eof()){
替换为
while(输入(in,customer\u record)){