Qt读写文件的简单封装

news/2024/7/7 8:35:50
C#中,有下列函数可以简单地读写文件:
读:  temp = File.ReadAllText("abc.txt",Encoding.Default);  
写:  File.WriteAllText("abc.txt", temp, Encoding.Default);
追加: File.AppendAllText("abc.txt", temp, Encoding.Default);


现在我也来用Qt彷写一个,以后读写简单的文本文件就不用这么麻烦啦。

#include <QtCore>

class RWFile
{
public:
	/*
	fileName: 要读写的文件名
	text: 要写入(或被写入的字符串)
	codec: 文字编码
	返回值: 失败就返回false, 成功则返回true
	*/
	static bool ReadAllText(const QString &fileName, QString &text,
		const char *codec=NULL);
	static bool WriteAllText(const QString &fileName, const QString &text,
		const char *codec=NULL);
	static bool AppendAllText(const QString &fileName, const QString &text,
		const char *codec=NULL);
};

bool RWFile::ReadAllText(const QString &fileName, QString &text, const char *codec)
{
	QFile file(fileName);
	if( !file.open(QIODevice::ReadOnly) )
		return false;
	QTextStream in(&file);
	if( codec != NULL )
		in.setCodec(codec);
	text = in.readAll();
	file.close();
	return true;
}

bool RWFile::WriteAllText(const QString &fileName, const QString &text, const char *codec)
{
	QFile file(fileName);
	if( !file.open(QIODevice::WriteOnly) )
		return false;
	QTextStream out(&file);
	if( codec != NULL )
		out.setCodec(codec);
	out << text;
	file.close();
	return true;
}

bool RWFile::AppendAllText(const QString &fileName, const QString &text, const char *codec)
{
	QFile file(fileName);
	if( !file.open(QIODevice::Append) )
		return false;
	QTextStream out(&file);
	if( codec != NULL )
		out.setCodec(codec);
	out << text;
	file.close();
	return true;
}

int main(int argc, char **argv)
{
	QCoreApplication app(argc, argv);
	QString temp("abcde");
	bool ok1, ok2, ok3;
	ok1 = RWFile::WriteAllText("abc.txt", temp, "UTF-8");
	ok2 = RWFile::AppendAllText("abc.txt", temp, "UTF-8");
	ok3 = RWFile::ReadAllText("abc.txt", temp, "UTF-8");
	if( ok1 && ok2 && ok3 )
	{
		qDebug() << temp;
	}
	return 0;
}



http://www.niftyadmin.cn/n/3658777.html

相关文章

web http方法

Post&#xff08;新增&#xff09;,Put&#xff08;修改&#xff09;,Delete&#xff08;删除&#xff09;,Get&#xff08;查询&#xff09; GET&#xff1a;生到数据列表&#xff08;默认&#xff09;&#xff0c;或者得到一条实体数据 POST&#xff1a;添加服务端添加一条…

将项目托管到gitHub

一、下载并安装Git版本控制工具 下载地址&#xff1a;https://git-scm.com/downloads 注册GitHub账号&#xff1a;https://github.com/ 为什么托管到GitHub要下载Git&#xff1f; git是一个版本控制工具   github是一个用git做版本控制的项目托管平台。 二、在IEDA中设置G…

C++中对文件进行读写操作

#include "stdafx.h"#include#include#includeusing namespace std;//从键盘上读取字符的函数void read_save(){ char c[80]; ofstream outfile("f1.dat"); //以输出方工打开文件 if(!outfile){ cerr<<"open error!"<exit(1); }…

降级论:智商高的IT人,你比沙县老板强吗

从前有三个屌丝&#xff0c;聚在一起做网络&#xff0c;提供免费的网络服务&#xff0c;砸锅卖铁&#xff0c;通宵达旦&#xff0c;除了卖肾啥都做了。3年后终于做到了五百万用户&#xff0c;对于年轻人来说&#xff0c;能把五百万人玩弄于鼓掌之间&#xff0c;已经是很牛逼轰轰…

用指向基类对象的指针输出数据

#include "stdafx.h"#include#includeusing namespace std;class student{private: int num; int age; float score;public: student(int ,int ,float); void display();};//定义构造函数 student::student(int n,int a,float sc):num(n),age(a),score(sc){}void stu…

Number of Under-Replicated Blocks问题

跑了一个mapreduce发现集群上出现了7个Under-Replicated Blocks&#xff0c;在web页面上能看到&#xff0c;在主节点上执行&#xff1a; $ bin/hadoop fsck -blocks 删除导致问题的文件之后就好了。 导致这个问题可能有俩个原因 1. 可能我们默认得block副本因子是3&#xf…

Ambari2.7.0离线部署

一、下载安装包 wget http://public-repo-1.hortonworks.com/ambari/centos7/2.x/updates/2.7.0.0/ambari-2.7.0.0-centos7.tar.gz wget http://public-repo-1.hortonworks.com/HDP-UTILS-1.1.0.22/repos/centos7/HDP-UTILS-1.1.0.22-centos7.tar.gz wget http://public-rep…

《Unix环境高级编程》:打印指定的描述符的文件标志

《Unix环境高级编程》这本书附带了许多短小精美的小程序&#xff0c;我在阅读此书的时候&#xff0c;将书上的代码按照自己的理解重写了一遍&#xff08;大部分是抄书上的&#xff09;&#xff0c;加深一下自己的理解&#xff08;纯看书太困了&#xff0c;呵呵&#xff09;。此…