想到了多周目游戏,一开始在文件最开始,在编译预处理前面加了个int a,后来发现还是不行,每次程序结束,内存释放,就没有了。
到网上搜了一下,暂时没发现什么好的方法,都是在外边存一个文件.dat或者.txt。
我试了一下,挺简单。
计数器:
#include<iostream>
#include<fstream>
#include<time.h>
#include<windows.h>
using namespace std;
int main(){
ofstream outfile;
ifstream infile;
int tem;
infile.open("计数器.dat");
infile>>tem;
tem++;
infile.close();
outfile.open("计数器.dat");
outfile<<tem;
outfile.close();
cout<<"This is the "<<tem<<" times run."<<endl;
system("pause");
}
归零:(初始化,让记录数变为零
#include<iostream>
#include<fstream>
using namespace std;
int main(){
ofstream outfile;
outfile.open("计数器.dat");
int a=0;
outfile<<a<<endl;
outfile.close();
}