最近在做QT东西时遇到在指定路径下创建文件,发现qt中没有直接用的。
主要通过自定义一个createFile()函数来实现,其中需要用到头文件。
为了测试方便,使用QDebug来进行信息点输出。
void createFile(QString filePath,QString fileName)
{
QDir tempDir;
//临时保存程序当前路径
QString currentDir = tempDir.currentPath();
//如果filePath路径不存在,创建它
if(!tempDir.exists(filePath))
{
qDebug()<
tempDir.mkpath(filePath);
}
QFile *tempFile = new QFile;
//将程序的执行路径设置到filePath下
tempDir.setCurrent(filePath);
qDebug()<
//检查filePath路径下是否存在文件fileName,如果停止操作。
if(tempFile->exists(fileName))
{
qDebug()<
return ;
}
//此时,路径下没有fileName文件,使用下面代码在当前路径下创建文件
tempFile->setFileName(fileName);
if(!tempFile->open(QIODevice::WriteOnly|QIODevice::Text))
{ <