700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Qt写入读取txt文本文件

Qt写入读取txt文本文件

时间:2020-11-25 16:14:20

相关推荐

Qt写入读取txt文本文件

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

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

写入

覆盖写入

QFile f("D:\\qtManager.txt");if(!f.open(QIODevice::WriteOnly | QIODevice::Text)){qDebug() << ("打开文件失败");}QTextStream txtOutput(&f);QString str = "123";txtOutput << str << endl;f.close();

文末写入

QFile f("D:\\qtManager.txt");if(!f.open(QIODevice::ReadWrite | QIODevice::Append)) //以读写且追加的方式写入文本{qDebug() << ("打开文件失败");}QTextStream txtOutput(&f);QString str = "123";txtOutput << str << endl;f.close();

读取

QFile f("D:\\qtManager.txt");if(!f.open(QIODevice::ReadOnly | QIODevice::Text)){qDebug() << ("打开文件失败");}QTextStream txtInput(&f); QString lineStr;while(!txtInput.atEnd()){lineStr = txtInput.readLine();qDebug() << (lineStr);}f.close();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。