Qt 读写txt文本文件


打开文件时,使用参数选择打开文件模式

需要导入QFile和qDebug、QString头文件

写入

覆盖写入

1 QFile f("D:\\qtManager.txt"); 2 if(!f.open(QIODevice::WriteOnly | QIODevice::Text)) 3 6 QTextStream txtOutput(&f); 7 QString str = "123"; 8 txtOutput << str << endl; 9 flose();

文末写入

1 QFile f("D:\\qtManager.txt"); 2 if(!f.open(QIODevice::ReadWrite | QIODevice::Append)) //以读写且追加的方式写入文本 3 6 QTextStream txtOutput(&f); 7 QString str = "123"; 8 txtOutput << str << endl; 9 flose();

读取

1 QFile f("D:\\qtManager.txt"); 2 if(!f.open(QIODevice::ReadOnly | QIODevice::Text)) 3 6 QTextStream txtInput(&f); 7 QString lineStr; 8 while(!txtInput.atEnd()) 9 13 flose();



上一篇:DALSA工业相机SDK二次开发(图像采集及保存)C#

下一篇:Qt QVariant的用法


Qt
Copyright © 2002-2019 k262电脑网 www.k262.cn 皖ICP备2020016292号
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!QQ:251442993 热门搜索 网站地图