zoukankan      html  css  js  c++  java
  • [Qt] 文本文件读写, 摘自官方文档

    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 (" ") into C++-style terminators (" "). 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: " << 49 << "
    ";
  • 相关阅读:
    如何用vue做计算器功能
    js反弹运动
    $.each的使用
    js文字滚动事件
    根据服务器时间,计算出时间轴的倒计时。
    时间格式转时间戳的几种方法
    匀速运动升级
    js匀速运动
    js图片滚动无缝衔接
    webFrame
  • 原文地址:https://www.cnblogs.com/liujx2019/p/10811370.html
Copyright © 2011-2022 走看看