保存用户数据以便后续运行程序。 [英] Saving user data for subsequent runs of a program.

查看:74
本文介绍了保存用户数据以便后续运行程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在正试图找到一种方法来保存用户数据(包括一些整数,tm值的映射和类值的映射),但我不知道如何去做。我尝试过的一种方法是将数据保存到数据文件中:

 int main()
{
string name;
int age;
bool test = false;
字符串输入;
ofstream saveData;
ifstream takeData;
std :: time_t tt = system_clock :: to_time_t(system_clock :: now());
tm curtime = * localtime(& tt);
tm time = curtime;
takeData.open(" data.dat");
takeData>>名称;
takeData.close();
if(name ==""){
name =" blank" ;;
}
cout<< "名称是" <<名称<< ENDL;
takeData.open(" data2.dat");
takeData>>年龄;
takeData.close();
if(age< 0){
age = 0;
}
cout<< "年龄是" <<年龄<< ENDL;
takeData.open(" data3.dat");
takeData>>测试;
takeData.close();
if(test == true){
cout<< "真"。 << ENDL;
} else {
cout<< "假QUOT。 << ENDL;
}
takeData.open(" data4.dat");
takeData>>时间;
takeData.close();
cout<< "输入时间是" << asctime(安培;时间);
cout<< "你的名字?" << ENDL;
cin>>名称;
saveData.open(" data.dat");
saveData<<名称;
saveData.close();
cout<< "你的年龄?" << ENDL;
cin>>年龄;
saveData.open(" data2.dat");
saveData<<年龄;
saveData.close();
cout<< "这是真的吗? (Y / N)" << ENDL;
cin>>输入;
if(input ==" y"){
test = true;
} else {
test = false;
}
cout<< "设定时间到12月1日。" << ENDL;
time.tm_mon = 11;
time.tm_mday = 1;
saveData.open(" data4.dat");
saveData<<时间;
saveData.close();
返回0;
}

这只是一个测试,它似乎一直有效,直到我使用了tm值,此时我得到了错误:


错误   &NBSP; C2679&NBSP;&NBSP;   binary'>>':找不到带有'tm'类型右手操作数的运算符(或者没有可接受的转换)


我试图保存的数据是:


map< string,Date *> m_dates;
$
map< string,Tracker *> m_tasks;
$
map< string,vector< tm>> m_taskreports;
$
map< string,tm> m_datereports;

map< string,Goal *> m_goals;


以及4个整数。有没有一种简单的方法可以在用户关闭程序时保存这些数据,然后在再次打开程序时将其恢复?

解决方案

您需要将struct tm转换为time_t值(使用mktime),可以将其写入并读取为整数。 或者,您可以将其转换为ISO时间字符串("2019-03-26T14:19:00")。


您在此处执行的任务称为"序列化"。和"反序列化",如果你想进行一些网络搜索。


正确答案取决于很多事情。 您是否希望能够手动编辑这些文件? 如果是这样,那么您可以考虑以JSON格式编写数据:

 {
" dates":[
{
" name":" tuesday",
" date":15536​​35007,
" date2":" 2019-03-26T14:17:00"
},
{
" name":" thursday",
" date":1553807876,
" date2":" 2019-03 -28T14:17:00"
}
]
}


或者,你可以用二进制格式写出来。 只要你写出一些东西告诉你接下来会发生什么,你就应该能够把它读回去。




So I'm currently trying to find a way to save user data (which consists of some integers, maps of tm values, and maps of class values), but I'm not sure how to go about it. One method I tried was saving data to a data file:

int main()
{
	string name;
	int age;
	bool test = false;
	string input;
	ofstream saveData;
	ifstream takeData;
	std::time_t tt = system_clock::to_time_t(system_clock::now());
	tm curtime = *localtime(&tt);
	tm time = curtime;
	takeData.open("data.dat");
	takeData >> name;
	takeData.close();
	if (name == "") {
		name = "blank";
	}
	cout << "Name is " << name << endl;
	takeData.open("data2.dat");
	takeData >> age;
	takeData.close();
	if (age < 0) {
		age = 0;
	}
	cout << "Age is " << age << endl;
	takeData.open("data3.dat");
	takeData >> test;
	takeData.close();
	if (test == true) {
		cout << "True." << endl;
	} else {
		cout << "False." << endl;
	}
	takeData.open("data4.dat");
	takeData >> time;
	takeData.close();
	cout << "The input time is " << asctime(&time);
	cout << "Your name?" << endl;
	cin >> name;
	saveData.open("data.dat");
	saveData << name;
	saveData.close();
	cout << "Your Age?" << endl;
	cin >> age;
	saveData.open("data2.dat");
	saveData << age;
	saveData.close();
	cout << "Is it true? (y/n)" << endl;
	cin >> input;
	if (input == "y") {
		test = true;
	} else {
		test = false;
	}
	cout << "Setting time to december 1st." << endl;
	time.tm_mon = 11;
	time.tm_mday = 1;
	saveData.open("data4.dat");
	saveData << time;
	saveData.close();
	return 0;
}

This was just a test, and it seemed to be working until I used tm values, at which point I got the error:

Error    C2679    binary '>>': no operator found which takes a right-hand operand of type 'tm' (or there is no acceptable conversion)

The data I'm try to save is:

map<string, Date*> m_dates;
map<string, Tracker*> m_tasks;
map<string, vector<tm>> m_taskreports;
map<string, tm> m_datereports;
map<string, Goal*> m_goals;

Along with 4 integers. Is there a simple way I could save this data when the user closes the program and then restore it when they open it up again?

解决方案

You need to convert the struct tm to a time_t value (using mktime), which can be written and read as an integer.  Or, you can convert it to an ISO time string ("2019-03-26T14:19:00").

The task you're doing here is called "serialization" and "deserialization", if you want to do some web searching.

The right answer depends on many things.  Do you want to be able to edit these files by hand?  If so, then you might consider writing the data in something like JSON format:

{
    "dates": [
        {
            "name": "tuesday",
            "date": 1553635007,
            "date2": "2019-03-26T14:17:00"
        },
        {
            "name": "thursday",
            "date": 1553807876,
            "date2": "2019-03-28T14:17:00"
        }
    ]
}

Alternatively, you can write it all in a binary format.  As long as you write out something that tells you what's coming next, you should be able to read it back in.



这篇关于保存用户数据以便后续运行程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆