File C++;未永久保存的文件

File C++;未永久保存的文件,file,File,我刚刚制作了一个简单的程序,基本上将数字保存到用户输入的程序中,并将其保存到名为data.txt的文件中,该值是累积的,因此当用户再次输入另一个数字时,将调用旧值进行某些操作,您将在下面看到: #include <iostream> #include <fstream> #include <time.h> #include <stdio.h> #include <stdlib.h> #include <ctype.h> u

我刚刚制作了一个简单的程序,基本上将数字保存到用户输入的程序中,并将其保存到名为data.txt的文件中,该值是累积的,因此当用户再次输入另一个数字时,将调用旧值进行某些操作,您将在下面看到:

#include <iostream>
#include <fstream>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

using namespace std;

int main(){
ifstream input;
ofstream output, invoice;

// initializations
int total = 0, x = 0;
double pay = 0, rate = 0.1008;
char choice = ' ';


// time code
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
// timecode 


// Open data.txt to get total
invoice.open("invoice.txt", fstream::out|fstream::app);
input.open("data.txt");
if(input.is_open()){
    input>>total;
}else{
    cout<<"Error file hasn't been opened!";
    return 0;
}
input.close();
// Modify data.txt
output.open("data.txt");
if(output.is_open()){
    mistake1:
    mistake2:
    cout<<"\t\t\tTotal hours from last time:"<<total<<endl;
    cout<<"\tDate: "<<asctime(timeinfo);
    cout<<"Enter hours: ";
    cin>>x;
    if(x<=total){
        cout<<"Error! re-enter the hours (they can't be less than or equal to "<<total<<"):"<<endl<<endl;
        goto mistake1;
    }
    cout<<"The value you've entered is "<<x<<", correct? (answer with Y or N):"<<endl;
    cin>>choice;
    if(choice == 'N' || choice == 'n'){
        goto mistake2;
    }
    if(total == 0){
        total=x;
        pay = x*rate;
        cout<<"----------------------------------------------------------"<<endl;
        cout<<"\tDate: "<<asctime(timeinfo);
        cout<<endl<<"\t\t*Information*"<<endl<<endl;
        cout<<"\tTotal hours used: "<<x<<endl<<endl;
        cout<<"\tYou have to pay ( "<<pay<<" $) this month."<<endl;
        cout<<"----------------------------------------------------------";
        invoice<<"----------------------------------------------------------"<<endl;
        invoice<<"\tDate: "<<asctime(timeinfo);
        invoice<<endl<<"\t\t*Information*"<<endl<<endl;
        invoice<<"\tTotal hours used: "<<x<<endl<<endl;
        invoice<<"\tYou have to pay ( "<<pay<<" $) this month."<<endl;
        invoice<<"----------------------------------------------------------"<<endl;
    }else{
        pay = (x-total)*rate;
        cout<<"----------------------------------------------------------"<<endl;
        cout<<"\tDate: "<<asctime(timeinfo);
        cout<<endl<<"\t\t*Information*"<<endl<<endl;
        cout<<"\tTotal hours used: "<<x<<endl<<endl;
        cout<<"\tYou have to pay ( "<<pay<<" $) this month."<<endl<<endl;
        cout<<"\tYou have used the AC "<<x-total<<" hour/s this month!"<<endl;
        cout<<"----------------------------------------------------------";
        invoice<<"----------------------------------------------------------"<<endl;
        invoice<<"\tDate: "<<asctime(timeinfo);
        invoice<<endl<<"\t\t*Information*"<<endl<<endl;
        invoice<<"\tTotal hours used: "<<x<<endl<<endl;
        invoice<<"\tYou have to pay ( "<<pay<<" $) this month."<<endl<<endl;
        invoice<<"\tYou have used the AC "<<x-total<<" hour/s this month!"<<endl;
        invoice<<"----------------------------------------------------------"<<endl;
        total=x;
    }
    output<<total;
}
output.close();
cout<<endl<<endl<<"Enter any key to exit or press the X button...";
cin>>choice;
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
ifstream输入;
流输出、发票;
//初始化
整数总计=0,x=0;
双薪=0,费率=0.1008;
字符选择=“”;
//时间码
时间与时间;
结构tm*时间信息;
时间(&rawtime);
timeinfo=localtime(&rawtime);
//时间码
//打开data.txt以获取总计
发票打开(“invoice.txt”,fstream::out | fstream::app);
打开(“data.txt”);
if(input.is_open()){
输入>>总计;
}否则{

cout您可能希望为所有
open(“data.txt”)
调用添加正确的openmode。只需为`input.open(“data.txt”)添加
fstream::in
。它应该可以工作


原因是每次代码打开data.txt文件时,您首先打开以读取数据。然后打开以写入数据。当输入操作中不存在文件时,您需要处理第一种情况。您的代码应该可以工作。

您需要为此操作提供文件打开模式。例如,在你的情况就是这样

output.open ("data.txt", std::fstream::out | std::fstream::app);

这应该符合您的期望。

这就是它的功能,我希望它像覆盖它一样,因此每次用户输入值时,旧值都会消失并替换为新值,这就是我所做的我第一次打开程序并放入25,然后再次打开并放入,它变成了2550,谢谢!这就是它的功能,I.Imgur.com/lYgWHTV.png我希望它像覆盖它一样,所以每次用户输入一个值时,旧的值都会消失并替换为新的值,下面是我所做的我第一次打开程序并放入25,然后再次打开并放入,它变成了2550 I.imgur.com/UcN6kbZ.png,谢谢!编辑了答案