简体   繁体   English

在C ++中将时间保存/写入txt文件

[英]saving/writing time to txt file in C++

I wanted to save the time into the existing txt file so that I can know when this particular record is added..I have this code to auto detect the time 我想将时间保存到现有的txt文件中,以便可以知道何时添加此特定记录。.我有这段代码可以自动检测时间

time_t Now1;
struct tm * timeinfo;
char time1[20];
time(&Now1);
timeinfo = localtime(&Now1);
strftime(time1, 20, "%d/%m/%Y %H:%M", timeinfo);
Passports.Record_Added_On = time1;
cout << "\n\nRecord Added On: " << Passports.Record_Added_On;

code of reading the txt file: 读取txt文件的代码:

fs = new fstream(Passports_FILE_NAME, ios::in | ios::out | ios::binary);
            if (!fs)
            {
                cout << "\n Can't open or create '" << Passports_FILE_NAME << "' file" << "\n";
                system("pause");
                break;
            }
            recs_num = -1;
            while (fs->read((char *)&Passports, sizeof(Passports)))
            {
                recs_num++;
                if (Passports.ID == id && !Passports.Deleted)
                break;
            }
            if (fs->eof()) //if (the record is not in the file || it's there but it's Deleted)
            {
                cout << "\nThe specific passports record does not exists in the file.";
                closeFile(fs);
                cout << "\n\nExit\n";
                return 0;
            }

it worked fine even when I display..however when I closed the program and open it again.. it shows weird characters like this or sometimes crashes..can anyone help me on this and explain what is the reason behind it? 即使我显示它,它也可以正常工作。但是,当我关闭程序并再次打开它时,它却显示出这种奇怪的字符,有时甚至崩溃了。有人可以帮助我,解释其背后的原因是什么?

在此处输入图片说明

You cannot initialize non POD objects ( like std::string class ) from memory. 您不能从内存中初始化非POD对象(如std :: string class)。 This leads to crashes because class does not know it is initialized. 这会导致崩溃,因为类不知道它是否已初始化。 There is no guarantee that all class members where properly initialized from this code. 无法保证所有此类成员均已从此代码正确初始化。 Suppose that there was some class member which was pointer to some memory, allocated for you data. 假设有一个类成员,该成员指向一些为您的数据分配的内存。 You saved it to file, then run your application again to load it. 您将其保存到文件中,然后再次运行您的应用程序以加载它。 Then the pointer gets the same numeric value, but there is no more memory which he was pointing at. 然后,指针将获得相同的数值,但没有更多的内存指向他。 The memory must be allocated by OS, after OS function call, which was not called in our case. 调用OS函数后,必须由OS分配内存,在本例中未调用该内存。 So next time you try to use this badly initialized class object - it crashed because it tries to work with memory which does not belongs to your application. 因此,下次您尝试使用此初始化不佳的类对象时,它会崩溃,因为它尝试使用不属于您的应用程序的内存。 To fix this crash you should parse the file contents properly and then fill your Passports record accordingly. 要解决此崩溃,您应该正确解析文件内容,然后相应地填写您的Passports记录。 For example: 例如:

int int_value;
std::string string_value;
fs >> int_value >> string_value;

after that you can initialize your object: 之后,您可以初始化您的对象:

Passports.ID = int_value;
Passports.Name = string_value;

etc. The same way you should use to save data to the file ( this is called serialization, i recommend you search more on the topic ). 等。您应该使用相同的方式将数据保存到文件中(这称为序列化,我建议您在该主题中搜索更多内容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM