创建文件夹
void udp::Create_a_folder()
{
QString absolutePath = "D:/";
QString dirName = absolutePath+"/"+QDateTime::currentDateTime().toString("文件夹名称");
QDir dir(dirName);
if(!dir.exists())
dir.mkdir(dirName);
else
return;
}
在以上文件夹中创建文件
void udp::Create_a_file()
{
QString strTime = "文件昵称";
filename =dirName+"/"+strTime+".txt";//文件路径
QFile file(filename);
file.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append);
file.close();
}
获取文件中内容的行数
int udp::Read()
{
QFile file(filename);//filename为文件路径
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 0;
int count = 0;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
count++;
}
return count;
}
读取文件中的内容
void udp::Read_the_content()
{
int ns = Read();
int n = 0;
QFile file(filename);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QString line1;
QTextStream in(&file); //用文件构造流
while(n < ns)
{
line1 = in.readLine();//读取一行放到字符串里
qDebug()<<line1;
n++;
}
file.close();
}
}
写入数据
void udp::wfile()//写入数据
{
QString str = "要写入的数据";
QFile file(filename);
file.open(QFile::WriteOnly|QFile::Truncate);
file.close();
file.open(QIODevice::ReadWrite|QIODevice::Text|QIODevice::Append);
file.write(str.toLocal8Bit());//连续写入
file.write(QString(str+"\n").toLocal8Bit());//换行写入
file.close();
}