[Qt] 文本文件读写, 摘自官方文档
Reading Files DirectlyThe following example reads a text file line by line: QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; while (!file.atEnd()) { QByteArray line = file.readLine(); process_line(line); } The QIODevice::Text flag passed to open() tells Qt to convert Windows-style line terminators ("rn") into C++-style terminators ("n"). By default,QFile assumes binary,i.e. it doesn‘t perform any conversion on the bytes stored in the file. ? Using Streams to Read FilesThe next example uses QTextStream to read a text file line by line: QFile file("in.txt"); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream in(&file); while (!in.atEnd()) { QString line = in.readLine(); process_line(line); } QTextStream takes care of converting the 8-bit data stored on disk into a 16-bit Unicode QString. By default,it assumes that the user system‘s local 8-bit encoding is used (e.g.,ISO 8859-1 for most of Europe; see QTextCodec::codecForLocale() for details). This can be changed using setCodec(). To write text,we can use operator<<(),which is overloaded to take a QTextStream on the left and various data types (including QString) on the right: QFile file("out.txt"); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out(&file); out << "The magic number is: " << 49 << "n"; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-phone-7 – Windows Phone 7仿真器位置数据格式
- windows – 防止以纯文本形式获取IIS物理路径凭据(使用powe
- windows – 命令提示符:文件大小大于1024KB的dir文件
- Windows XP上的不可移动文件
- windows-service – 在LocalService帐户下运行的Win Servic
- Windows平台下搭建自己的Git服务器
- 批处理文件 – 如何将今天的日期与文件的上次修改日期进行比
- 如何在带有windows的apache上安装mod_pagespeed,以及从哪里
- 在设计WinForms UI时尊重XP主题
- windows server 2012 R2修改默认远程端口