C ++文件未永久保存

时间:2018-04-27 17:26:23

标签: file

我刚刚创建了一个简单的程序,基本上将数字保存到用户输入的程序中并将其保存到名为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;
}

代码很好并且工作正常,但问题出现了。 This (hours) value is called from the data.txt at first, it is defaulted to zero so everything is still fine (look at the date and time)

Now I checked data.txt and the value is saved there as wanted.

I reopened the program after seconds and everything is saved as wanted.

Then after few seconds I opened again (it is weird sometimes it takes long to do this (hours) and sometimes it takes (seconds) ) the value was gone and data.txt is empty it is not even zero just empty

有谁知道如何解决这个奇怪的问题:S? 谢谢!

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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

这应该符合您的期望。