1.定义结构体
#pragma pack(1) //字节对齐
struct ALARM_FILE
{
char STATUS_DATA[9];
char STATUS_TIME[7];
int AlarmNo;
int MONITOR_ALARM_FLAG;
};
ALARM_FILE alarmFile;
2.写二进制文件
ALARM_FILE alarmFile;
//时间和日期
QDateTime currentDataTime=QDateTime::currentDateTime();
QString data=currentDataTime.toString("yyyyMMdd");
QString time=currentDataTime.toString("hhmmss");
//日期
char *pChar=data.toLatin1().data();
strcpy(alarmFile.STATUS_DATA,pChar);
//时间
pChar=time.toLatin1().data();
strcpy(alarmFile.STATUS_TIME,pChar);
//报警号,报警标志
alarmFile.AlarmNo=123;
alarmFile.MONITOR_ALARM_FLAG=4556;
// 检查报警文件路径,不存在则新建
QString path=QApplication::applicationDirPath()+"/AlarmFile";
QDir dir(path);
if(!dir.exists()){
dir.mkdir(path);
}
//站点号m_strSiteCode
//时间 "yyyy-MM-dd:hh:mm:ss"
QDateTime::currentDateT

本文介绍了如何使用QT库进行二进制文件的读写操作,包括定义结构体,写入数据到二进制文件以及从二进制文件中读取数据的详细步骤。
最低0.47元/天 解锁文章
550

被折叠的 条评论
为什么被折叠?



