Reading Files Directly

The 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 ("\r\n") 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 Files

The 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: " << << "\n";

[Qt] 文本文件读写, 摘自官方文档的更多相关文章

  1. Kooboo中怎么写Page Plugin -摘自官方文档

    Page plugin development Page plugin is an add-on to Kooboo CMS, and is responsible for making data s ...

  2. Qt元类型(MetaType)注册入门(附一些官方文档的关键摘录)

    昨天调试项目时,突然发现如下消息: QObject::connect: Cannot queue arguments of type 'ERROR_LEVEL' (Make sure 'ERROR_L ...

  3. cassandra 3.x官方文档(7)---内部原理之如何读写数据

    写在前面 cassandra3.x官方文档的非官方翻译.翻译内容水平全依赖本人英文水平和对cassandra的理解.所以强烈建议阅读英文版cassandra 3.x 官方文档.此文档一半是翻译,一半是 ...

  4. Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇)

    目录 Mysql优化(出自官方文档) - 第九篇(优化数据库结构篇) 1 Optimizing Data Size 2 Optimizing MySQL Data Types 3 Optimizing ...

  5. Spark官方文档 - 中文翻译

    Spark官方文档 - 中文翻译 Spark版本:1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 引入Spark(Linki ...

  6. Spark SQL 官方文档-中文翻译

    Spark SQL 官方文档-中文翻译 Spark版本:Spark 1.5.2 转载请注明出处:http://www.cnblogs.com/BYRans/ 1 概述(Overview) 2 Data ...

  7. Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南

    Protocol Buffers(Protobuf) 官方文档--Protobuf语言指南 约定:为方便书写,ProtocolBuffers在下文中将已Protobuf代替. 本指南将向您描述如何使用 ...

  8. Spark Streaming官方文档学习--上

    官方文档地址:http://spark.apache.org/docs/latest/streaming-programming-guide.html Spark Streaming是spark ap ...

  9. OpenGL ES着色器语言之变量和数据类型(一)(官方文档第四章)和varying,uniform,attribute修饰范围

    OpenGL ES着色器语言之变量和数据类型(一)(官方文档第四章)   所有变量和函数在使用前必须声明.变量和函数名是标识符. 没有默认类型,所有变量和函数声明必须包含一个声明类型以及可选的修饰符. ...

随机推荐

  1. Spring-Cloud-Netflix-Eureka注册中心

    TOC 概述 eureka是Netflix的子模块之一,也是一个核心的模块 eureka里有2个组件: 一个是EurekaServer(一个独立的项目) 这个是用于定位服务以实现中间层服务器的负载平衡 ...

  2. Three中的动画实现-[three.js]

    Table Of Content 动画原理 js中动画实现原理setInterval js中动画实现新方法requestAnimationFrame 一个示例 动画原理 动画的本质实际上就是快速地不断 ...

  3. Java 使用InputStream笔记

    当我们要从网络下载资源时,使用类似如下方法来获取InputStream实例: URLConnection connection = new URL("http://www.XXXX.XXX& ...

  4. Yum 软件仓库配置

    Yum 软件仓库的作用是为了进一步简化 RPM 管理软件的难度以及自动分析 所需软件包及其依赖关系的技术. 可以把 Yum 想象成是一个硕大的软件仓库,里面保存有几乎所 有常用的工具 . 第1步:进入 ...

  5. 查找 mysql 配置文件 my.cnf

    $ locate my.cnf 看看你的linux上有多少个my.cnf,一般都配置为/etc/my.cnf

  6. 【数据库】MySQL数据库(一)

    一.MySQL数据库系统 MySQL数据库系统就是用来对数据库.数据的一些管理 二.数据库系统 1.数据库 就是用来存储各种数据的 2.数据库管理系统 就是用来管理各种数据库的数据的一个系统 三.常见 ...

  7. 开学java的初步考试

    //第一个.java文件 package project1; //20183777 温学智 信1805-2 public class ScoreInformation { private String ...

  8. ECSHOP数据表结构完整仔细说明教程 (http://www.ecshop119.com/ecshopjc-868.html)

    s_account_log //用户账目日志表 字段 类型 Null 默认 注释 log_id mediumint(8) 否   自增ID号 user_id mediumint(8) 否   用户登录 ...

  9. Linq下有一个非常实用的SelectMany方法,很多人却不会用

    在平时开发中经常会看到有些朋友或者同事在写代码时会充斥着各种for,foreach,这种程式代码太多的话阅读性特别差,而且还显得特别累赘,其实在FCL中有很多帮助我们提高阅读感的方法,而现实中很多人不 ...

  10. Tcl编成第二天,set与unset

    代码如下: #!/usr/bin/tclsh set value "one" puts $value unset value puts $value set表示创建一个变量第一个参 ...