qt文件操作

读文件:

void readfile(QString filepath)
{
	QFile file(filepath);
	if (file.open(QFile::ReadOnly | QIODevice::Text))
	{
		while (!file.atEnd())
		{
			QByteArray data = file.readLine();
			QString str(data);
			qDebug()<

写文件:

void writefile(QString filename)
{
	QFile file(filename);
	if (file.open(QFile::WriteOnly | QIODevice::Text))
	{
		file.write("1\n");
		file.write("2\n");
		file.write("4\n");
		file.write("3\n");
		file.close();
	}
}
打印文件信息:
void qtfileInfo(QString s) //打印文件信息
{
    QFile file(s);
    QFileInfo info(file);

    qDebug() <<"filename:" << info.fileName();
    qDebug() <<"filepath:" << info.path();
    qDebug() <<"readable:" << info.isReadable();
    qDebug() <<"writable:" << info.isWritable();

    file.close();
}
创建临时文件对象:
QTemporaryFile tmpfile;   //创建临时文件对象

    if(tmpfile.open())
    {
        tmpfile.write("您好,我是临时文件哦\n");
        tmpfile.close();

    }
    QFileInfo finfo(tmpfile);
    qDebug() << finfo.fileName();
    qDebug() << finfo.filePath();
    qDebug() << finfo.created();

目录操作:

//获取当前目录
	QString currentpath = QDir::currentPath();
	QDir mdir("D:\\project\\3iSystemAlgo\\xiangguanzujian");
	qDebug() << mdir.exists();//测试是否存在
	qDebug() << mdir.absoluteFilePath("opticslifetime.cpp");//连接路径与文件名成为文件的绝对路径
	qDebug() << mdir.dirName();//剥离掉路径,只返回目录的名字:xiangguanzujian
	//获取目录的绝对路径
	QDir *dir = new QDir("D:\\MyBattery\\test");
	dir->cdUp();
	QString path = dir->absolutePath();
	qDebug()<< path;

遍历文件夹:

void foreachfile(QString path)
{
	QDir dir(path);
	foreach(QFileInfo fileinfo, dir.entryInfoList())
	{
		if (fileinfo.isFile())
		{
			qDebug() << fileinfo.fileName();
		}
		else
		{
			if (fileinfo.fileName() == "." || fileinfo.fileName() == "..")
				continue;
			qDebug() << fileinfo.absoluteFilePath();
			foreachfile(fileinfo.absoluteFilePath());
		}
	}
}

按数据大小读取文件:

QFile file("D:\\project\\Battery\\Battery\\RecipeConfig\\t.txt");
	file.open(QFile::ReadOnly);
	char buf[1024] = { 0 };
	file.read(buf,1024);
	printf("%s\n",buf);
	file.close();
QString tmpname = QDir::homePath() + QDir::separator();

按文件流的方式读:

void readfilestream(QString filename)
{
	QFile file(filename);
	if (!file.open(QFile::ReadOnly | QIODevice::Text))
		qDebug() << file.errorString();
	QTextStream in(&file);//创建输出流
	while (!in.atEnd())
	{
		QString oneLine = in.readLine();
		qDebug() << oneLine;
	}
}

按文件流的方式写:

void writeFileStream(QString filename)
{
	QFile file(filename);
	if (file.open(QFile::WriteOnly | QFile::Truncate))
	{
		QTextStream out(&file);//创建输入流
		out << "PI:" << qSetFieldWidth(10) << left << 3.1415926;//写入数据
		out << "\r\ninsert new line";//写入数据
	}
	file.close();
}
qByteArray:
void qByteArrayTest100(){
    //9:字节,说明这个字符数组是按字节来存放的
    QByteArray ba("刘关张");//utf-8:[a:1,, ]
    qDebug() << ba.size();//6

    //3:字符,说明string是根据字符编码来存放的
    QString str("刘关张");
    qDebug() << str.size();//6
}

/*
访问 QByteArray 主要有 4 中方式,分别为[]、at()、data[]和 constData[]。
其中[]和 data[]为可读可写,at()和 constData[]仅为可读。
如果仅是读,则通过 at()和 constData[]访问速度最快,因可避免复制处理。
*/
QByteArray ba;
	ba.resize(6);
	ba[0] = 0x3c;
	ba[1] = 0xb8;
	ba[2] = 0x64;
	ba[3] = 0x18;
	ba[4] = 0xca;
	ba.data()[5] = 0x31;
	qDebug() << "[]" << ba[2]; //[] d
	qDebug() << "at()" << ba.at(2); //at() d
	qDebug() << "data()" << ba.data()[2]; //data() d
	qDebug() << "constData()" << ba.constData()[2]; //constData() d


QByteArray strInt("1234");
    bool ok0;
    qDebug() << strInt.toInt();   // return 1234
    qDebug() << strInt.toInt(&ok0,16);   // return 4660, 默认把strInt作为16进制的1234,对应十进制数值为4660

    QByteArray string("1234.56");
    bool ok1;
    qDebug() << string.toInt();   // return 0, 小数均视为0
    qDebug() << string.toInt(&ok1,16);   // return 0, 小数均视为0
    qDebug() << string.toFloat();   // return 1234.56
    qDebug() << string.toDouble();   // return 1234.56

    QByteArray str("FF");
    bool ok2;
    qDebug() << str.toInt(&ok2, 16);     // return 255, ok2 == true
    qDebug() << str.toInt(&ok2, 10);     // return 0, ok == false, 转为十进制失败




QByteArray x("Qt by THE QT COMPANY");
    QByteArray y = x.toLower();
    // y == "qt by the qt company"
    qDebug() << "toLower():" << y;

    QByteArray z = x.toUpper();
    // z == "QT BY THE QT COMPANY"
    qDebug() << "toUpper():" << z;

缓冲区读写:

QByteArray array;
    QBuffer buffer(&array);
	if (buffer.open(QIODevice::WriteOnly))
	{
		QDataStream out(&buffer);
		out << QString("hello software");
		out << 3.14159;
	}
	buffer.close();
	if (buffer.open(QIODevice::ReadOnly))
	{
		QDataStream in(&buffer);
		QString str;
		double value;
		in >> str;
		in >> value;
		qDebug() << str;
		qDebug() << value;
	}

QByteArray 与 Hex 转换:

//把 Hex 编码转换为 char 存储到 QByteArray:
	QByteArray text = QByteArray::fromHex("517420697320677265617421");
	qDebug() << text.data();
	//把QByteArray转为Hex编码
	QByteArray byte;
	byte.resize(3);
	byte[0] = 0x30;
	byte[1] = 0x31;
	byte[2] = 0x32;
	qDebug() << byte.toHex();

QByteArray 字符串数值转为各类数值:

QByteArray data("84.56");
	qDebug() << data.toInt();
	qDebug() << data.toFloat();

QByteArray与QString之间的转换:

//QByteArray 转为 QString
	QByteArray ba("abc123");
	QString str = ba;
	//QByteArray 转为 QString
	QString str("abc123");
	QByteArray ba = str.toLatin1();

内存映射文件:文件直接从磁盘映射给进程,不需要读入内存

QFile file("D:\\project\\Battery\\Battery\\RecipeConfig\\t.txt");
	file.resize(64*1024);
	file.open(QIODevice::ReadWrite);
	uchar* ptr = file.map(0,64*1024);
	*ptr = 'a';//直接写,就像操作内存一样
	ptr[1] = 'b';
	ptr[2] = 'c';
	ptr[200] = 'd';
	ptr[1000] = 'm';
	ptr[2000] = 'e';
	//读
	uchar ch = *ptr;
	qDebug() << ch<

你可能感兴趣的:(qt,开发语言)