Project scenario:
In QT applications, sometimes it is necessary to record error information in a log file, or to output data to a file for comparison and viewing.
Create a log file, if the file exists, do not create it
QDir dir(QCoreApplication::applicationDirPath()+"/recv_data");
if(dir.exists())
{
}
else
{
dir.mkdir(QCoreApplication::applicationDirPath()+"/recv_data");//只创建一级子目录,即必须保证上级目录存在
}
QString filename = QCoreApplication::applicationDirPath()+"/recv_data/" +QString("%1").arg(QDateTime::currentDateTime().toString ("yyyyMMddhhmmss").toLatin1().data());
data_name =filename +".log";
Open file and write data
QFile file(data_name);
if (file.open(QIODevice::WriteOnly | QIODevice::Append))
{
}
QTextStream streamFile(&file);
streamFile << QString("%1").arg(data, 5, 'f', 2, QLatin1Char('0')) << " "<< << "\n" ;