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. QFile and binary writing problem
Forum Updated to NodeBB v4.3 + New Features

QFile and binary writing problem

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 733 Views 3 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.
  • Q Offline
    Q Offline
    qAlexKo
    wrote on last edited by qAlexKo
    #1

    A problem that I met today and I can't coup with it. I tried to write to a file a binary buffer (as the buffer is) using QFile, and I as the rezult I have a UTF-8 two byte pair text. What do I do wrong? (Linux, QtCreator)

    //test: write binary buffer to a file using QFile
    void Tmq_blf::on_actionQfile_binary_test_triggered()
    {
    string extASCII_dosstring("\x80\x81\x82\x83\x84\xa0\xa1\xa2\xa3\xa4");
    QFile qf;
    qf.setFileName("Qfiletst.bin");
    qf.open(QIODevice::WriteOnly);
    QTextStream qfs(&qf);
    
    qfs << extASCII_dosstring.c_str();
    }
    //------------------
    
    kkoehneK 1 Reply Last reply
    0
    • Q qAlexKo

      A problem that I met today and I can't coup with it. I tried to write to a file a binary buffer (as the buffer is) using QFile, and I as the rezult I have a UTF-8 two byte pair text. What do I do wrong? (Linux, QtCreator)

      //test: write binary buffer to a file using QFile
      void Tmq_blf::on_actionQfile_binary_test_triggered()
      {
      string extASCII_dosstring("\x80\x81\x82\x83\x84\xa0\xa1\xa2\xa3\xa4");
      QFile qf;
      qf.setFileName("Qfiletst.bin");
      qf.open(QIODevice::WriteOnly);
      QTextStream qfs(&qf);
      
      qfs << extASCII_dosstring.c_str();
      }
      //------------------
      
      kkoehneK Offline
      kkoehneK Offline
      kkoehne
      Moderators
      wrote on last edited by
      #2

      QTextStream tries to format the string in the right encoding (by default UTF-8, at least with Qt 6). If you want to write your data unmodified, use QDataStream.

      Director R&D, The Qt Company

      Christian EhrlicherC Q 2 Replies Last reply
      0
      • kkoehneK kkoehne

        QTextStream tries to format the string in the right encoding (by default UTF-8, at least with Qt 6). If you want to write your data unmodified, use QDataStream.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @kkoehne said in QFile and binary writing problem:

        If you want to write your data unmodified, use QDataStream.

        But QDataStream also adds some bytes to it for deserializing later on... using plain QFile::write() is the correct way here.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        Q 1 Reply Last reply
        3
        • kkoehneK kkoehne

          QTextStream tries to format the string in the right encoding (by default UTF-8, at least with Qt 6). If you want to write your data unmodified, use QDataStream.

          Q Offline
          Q Offline
          qAlexKo
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @kkoehne said in QFile and binary writing problem:

            If you want to write your data unmodified, use QDataStream.

            But QDataStream also adds some bytes to it for deserializing later on... using plain QFile::write() is the correct way here.

            Q Offline
            Q Offline
            qAlexKo
            wrote on last edited by qAlexKo
            #5

            @Christian-Ehrlicher said in QFile and binary writing problem:

            But QDataStream also adds some bytes to it for deserializing later on... using plain QFile::write() is the correct way here.

            Yes, QDatestream adds some bytes to the rezult. QFile::write works fine. Although, theoretically :), the streams must do it too. :)

            Anyway, thanks a lot.

            JonBJ 1 Reply Last reply
            0
            • Q qAlexKo

              @Christian-Ehrlicher said in QFile and binary writing problem:

              But QDataStream also adds some bytes to it for deserializing later on... using plain QFile::write() is the correct way here.

              Yes, QDatestream adds some bytes to the rezult. QFile::write works fine. Although, theoretically :), the streams must do it too. :)

              Anyway, thanks a lot.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @qAlexKo
              QDataStream writing is for another Qt program which uses QDataStream too for reading. The former writes type-bytes to the stream and the latter interprets them, to produce an object of a matching type.

              It does not interoperate with non-Qt programs, unless you use QDataStream::write/readRawData(). Here you can just use QFile::write().

              Q 1 Reply Last reply
              2
              • JonBJ JonB

                @qAlexKo
                QDataStream writing is for another Qt program which uses QDataStream too for reading. The former writes type-bytes to the stream and the latter interprets them, to produce an object of a matching type.

                It does not interoperate with non-Qt programs, unless you use QDataStream::write/readRawData(). Here you can just use QFile::write().

                Q Offline
                Q Offline
                qAlexKo
                wrote on last edited by qAlexKo
                #7

                A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                kkoehneK JonBJ M Kent-DorfmanK 4 Replies Last reply
                0
                • Q qAlexKo

                  A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                  kkoehneK Offline
                  kkoehneK Offline
                  kkoehne
                  Moderators
                  wrote on last edited by
                  #8

                  @JonB , you're of course right. QFile::write() is the correct choice.

                  Director R&D, The Qt Company

                  1 Reply Last reply
                  0
                  • Q qAlexKo

                    A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @qAlexKo said in QFile and binary writing problem:

                    I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not)

                    Not sure what this means. Maybe you mean integer handle?

                    The low-level system library write/read/seek etc. functions do work on an integer file descriptor. If you want to use these from a C++ or Qt program you can if you really want to, but nowadays a higher-level "handle" is preferred. Qt supplies QFile for this.

                    Q 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher moved this topic from C++ Gurus on
                    • JonBJ JonB

                      @qAlexKo said in QFile and binary writing problem:

                      I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not)

                      Not sure what this means. Maybe you mean integer handle?

                      The low-level system library write/read/seek etc. functions do work on an integer file descriptor. If you want to use these from a C++ or Qt program you can if you really want to, but nowadays a higher-level "handle" is preferred. Qt supplies QFile for this.

                      Q Offline
                      Q Offline
                      qAlexKo
                      wrote on last edited by qAlexKo
                      #10

                      @JonB said in QFile and binary writing problem:

                      Not sure what this means. Maybe you mean integer handle?
                      The low-level system library write/read/seek etc. functions do work on an integer file descriptor.

                      Yes handle
                      integer is like this
                      -----Beginning of the citation-----
                      io.h
                      int read(int handle, void *buf, unsigned len);
                      ----- The end of the citation -----

                      In QTcreator help I see only these

                      #include <cstdio>
                      int fread( void *buffer, size_t size, size_t num, FILE *stream );

                      JonBJ 1 Reply Last reply
                      0
                      • Q qAlexKo

                        A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                        M Offline
                        M Offline
                        mpergand
                        wrote on last edited by
                        #11

                        @qAlexKo said in QFile and binary writing problem:

                        I update an old C text where they used write/read/seek functions with an integer handler

                        Qfile has this:

                        bool QFile::open(int fd, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
                        
                        Q 1 Reply Last reply
                        0
                        • M mpergand

                          @qAlexKo said in QFile and binary writing problem:

                          I update an old C text where they used write/read/seek functions with an integer handler

                          Qfile has this:

                          bool QFile::open(int fd, QIODevice::OpenMode mode, QFileDevice::FileHandleFlags handleFlags = DontCloseHandle)
                          
                          Q Offline
                          Q Offline
                          qAlexKo
                          wrote on last edited by qAlexKo
                          #12

                          @mpergand said in QFile and binary writing problem:

                          Qfile has this:
                          bool QFile::open(

                          Thanks, although it will hardly help replace those functions, probably it easier to rewrite it completely.

                          1 Reply Last reply
                          0
                          • Q qAlexKo

                            @JonB said in QFile and binary writing problem:

                            Not sure what this means. Maybe you mean integer handle?
                            The low-level system library write/read/seek etc. functions do work on an integer file descriptor.

                            Yes handle
                            integer is like this
                            -----Beginning of the citation-----
                            io.h
                            int read(int handle, void *buf, unsigned len);
                            ----- The end of the citation -----

                            In QTcreator help I see only these

                            #include <cstdio>
                            int fread( void *buffer, size_t size, size_t num, FILE *stream );

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @qAlexKo said in QFile and binary writing problem:

                            int read(int handle, void *buf, unsigned len);

                            Yes this still exists. You probably need the right #include for your OS --- <cstdio> is not the right one. Linux tends to use #include <unistd.h> for these, not sure if you are Windows. If you are saying <io.h> is right for Windows that may be. Note that you might have to look for such functions with a leading _, i.e. it may be int _read(....).

                            However, as I say nowadays you would normally be expected to use std:: or Qt functions instead of the very low-level ones. Partly to ensure cross-platform.

                            1 Reply Last reply
                            0
                            • Q qAlexKo

                              A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                              Kent-DorfmanK Offline
                              Kent-DorfmanK Offline
                              Kent-Dorfman
                              wrote on last edited by Kent-Dorfman
                              #14

                              @qAlexKo said in QFile and binary writing problem:

                              A little unrelated question, if you don't mind: I update an old C text where they used write/read/seek functions with an integer handler (fopen, for instance, does not). I can't find similar functions in QTCreator programming. Do they exist there?

                              The functionality is in QIODevice and its subclass of QFileDevice. Read the docs carefully. QTCreator has nothing to do with the supported API. It is just an IDE.

                              1 Reply Last reply
                              0

                              • Login

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