簡單的問題:我想在檔案中寫入當前日期。以下是我的代碼:
void fileWR::write()
{
QFile myfile("date.dat");
if (myfile.exists("date.dat"))
myfile.remove();
myfile.open(QIODevice::ReadWrite);
QDataStream out(&myfile);
out << (quint8) QDate::currentDate().day(); // OK!!
out << (quint8) QDate::currentDate().month(); // OK!!
out << (quint8) QDate::currentDate().year(); // NOT OK !!!
myfile.close();
}
當我閱讀檔案時,我發現一個位元組代表日期(0x18 代表 24 日),一個位元組代表月份(0x02 代表 2 月)和一個錯誤位元組代表年份(0xe6 代表 2022 年)。我需要一年的最后兩個數字(例如:2022 -> 22)。我能怎么做?謝謝保羅
uj5u.com熱心網友回復:
十六進制的 2022 是 0x7E6 ,當您保存將其轉換為 uint8 時,最高有效位將被截斷以獲得您所指示的內容。想法是使用模塊運算子將 2022 轉換為 22,然后保存:
QDataStream out(&myfile);
QDate now = QDate::currentDate();
out << (quint8) now.day();
out << (quint8) now.month();
out << (quint8) (now.year() % 100);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434939.html
上一篇:如何僅在Qlabel中獲取滑鼠位置?(對于pyqt5)
下一篇:如何在qt容器中存盤qt容器
