Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. doubt reading files with qtextstream
Forum Updated to NodeBB v4.3 + New Features

doubt reading files with qtextstream

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jeronimo
    wrote on last edited by Jeronimo
    #1

    Hi normally i read the file so:

    log.setFileName(directorio.tempPath() + "/example");
                if(!log.exists())
                {
                    //no existe
                }else{
                    log.open(QFile::ReadOnly);
                    cliente.sendMessage(from,"read|@|" + log.readAll());
                    log.close();
                }
    

    But now i'm trying to read one file with text in utf-8 and at the moment i couldn't some idea how to solve it?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      What does it mean you "couldn't"? The file didn't open, nothing was read or you got something else than what you got?

      readAll() reads bytes, not characters. It's your responsibility to convert the bytes to correctly encoded string, for example using QString::fromUtf8().

      EDIT Sorry, my bad. I thought you were using QFile::readAll(), but it's QTextStream::readAll() which does return a string.

      J 1 Reply Last reply
      2
      • Chris KawaC Chris Kawa

        What does it mean you "couldn't"? The file didn't open, nothing was read or you got something else than what you got?

        readAll() reads bytes, not characters. It's your responsibility to convert the bytes to correctly encoded string, for example using QString::fromUtf8().

        EDIT Sorry, my bad. I thought you were using QFile::readAll(), but it's QTextStream::readAll() which does return a string.

        J Offline
        J Offline
        Jeronimo
        wrote on last edited by Jeronimo
        #3

        @Chris-Kawa said in doubt reading files with qtextstream:

        QString::fromUtf8().

        one question but for utf16(i'm changed the encoding) i tried this:
        const ushort* str = read_raw(log);
        QString qstr = QString::fromUtf16(str);

        But not seem's recognize read_raw, maybe this method is only used in qt 4.8?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          The answer is in the title of your question, use QTextStream to read the file, do not read the file directly.
          Also, since it's a textfile you shouldn't open it in binary mode. log.open(QFile::ReadOnly | QFile::Text );

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          J 1 Reply Last reply
          0
          • VRoninV VRonin

            The answer is in the title of your question, use QTextStream to read the file, do not read the file directly.
            Also, since it's a textfile you shouldn't open it in binary mode. log.open(QFile::ReadOnly | QFile::Text );

            J Offline
            J Offline
            Jeronimo
            wrote on last edited by
            #5

            @VRonin ok i must to put QFile::text if i put some extension like .txt. So i can't do the before example read all file and convert to utf16. ok.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              yes, you can:

              if(log.open(QIODevice::ReadOnly | QIODevice::Text)){
              QTextStream logStream(&log);
              logStream.setCodec("UTF-8");
              logStream.setAutoDetectUnicode(true);
                              cliente.sendMessage(from,"read|@|" + logStream.readAll());
                              log.close();
              }
              

              @Jeronimo said in doubt reading files with qtextstream:

              i must to put QFile::text if i put some extension like .txt

              The extension is meaningless, it is only a trick OSs use to distinguish different files. QIODevice::Text is used to

              When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.

              source http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved