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. Write on File
Forum Updated to NodeBB v4.3 + New Features

Write on File

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 5 Posters 4.8k 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.
  • A Offline
    A Offline
    Alexanov
    wrote on last edited by
    #1

    Hi
    I filled c (char *) with some data like this:

    //your code here
    char  *c;
    for(int i=0; i<5700;i++){
            c[i]=i;
        }
    

    then I want to write all data of 'c' on file with file.write(), But when i use this code my file is blank (no data in it).

    Although I don't want to use this method below, because it will take time and time is important factor for me :

    QTextStream stream(&file);
    for(int l=0; l<5700;l++){
            stream << c[l] << endl;
        }
    

    can you say me a solution?

    joeQJ VRoninV 2 Replies Last reply
    0
    • A Alexanov

      Hi
      I filled c (char *) with some data like this:

      //your code here
      char  *c;
      for(int i=0; i<5700;i++){
              c[i]=i;
          }
      

      then I want to write all data of 'c' on file with file.write(), But when i use this code my file is blank (no data in it).

      Although I don't want to use this method below, because it will take time and time is important factor for me :

      QTextStream stream(&file);
      for(int l=0; l<5700;l++){
              stream << c[l] << endl;
          }
      

      can you say me a solution?

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by joeQ
      #2

      @Alexanov hi, friend welcome.

      did you close the file of you open ?

      QFile file("out.txt");
            if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                return;
      
            QTextStream out(&file);
            out << "The magic number is: " << 49 << "\n";
      
          file.close(); ///< don't forget to close file.
      

      Just do it!

      1 Reply Last reply
      0
      • A Alexanov

        Hi
        I filled c (char *) with some data like this:

        //your code here
        char  *c;
        for(int i=0; i<5700;i++){
                c[i]=i;
            }
        

        then I want to write all data of 'c' on file with file.write(), But when i use this code my file is blank (no data in it).

        Although I don't want to use this method below, because it will take time and time is important factor for me :

        QTextStream stream(&file);
        for(int l=0; l<5700;l++){
                stream << c[l] << endl;
            }
        

        can you say me a solution?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        @Alexanov said in Write on File:

        char *c;

        That's a dangling pointer. you can't do anything with it. c[i]=i; messes up your memory, I'm surprised your OS doesn't just crash the app on the spot. on top of that you are overflowing the char type as it can't reach beyond 127.

        QByteArray c;
        for(int i=0; i<5700;++i)
        c.append(QByteArray::number(i));
        file.write(c);
        

        "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

        A 2 Replies Last reply
        4
        • VRoninV VRonin

          @Alexanov said in Write on File:

          char *c;

          That's a dangling pointer. you can't do anything with it. c[i]=i; messes up your memory, I'm surprised your OS doesn't just crash the app on the spot. on top of that you are overflowing the char type as it can't reach beyond 127.

          QByteArray c;
          for(int i=0; i<5700;++i)
          c.append(QByteArray::number(i));
          file.write(c);
          
          A Offline
          A Offline
          Alexanov
          wrote on last edited by
          #4

          @VRonin said in Write on File:

          That's a dangling pointer. you can't do anything with it. c[i]=i; messes up your memory, I'm surprised your OS doesn't just crash the app on the spot. on top of that you are overflowing the char type as it can't reach beyond 127.

          I can not understand what you said, because for allocating memory , I used this code and worked fine :

          why you said messes up my memory?

              c = (char *)malloc(sizeof(double)*5700);
          
          D 1 Reply Last reply
          0
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            http://www.cplusplus.com/doc/tutorial/dynamic/

            "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
            • A Alexanov

              @VRonin said in Write on File:

              That's a dangling pointer. you can't do anything with it. c[i]=i; messes up your memory, I'm surprised your OS doesn't just crash the app on the spot. on top of that you are overflowing the char type as it can't reach beyond 127.

              I can not understand what you said, because for allocating memory , I used this code and worked fine :

              why you said messes up my memory?

                  c = (char *)malloc(sizeof(double)*5700);
              
              D Offline
              D Offline
              Devopia53
              wrote on last edited by Devopia53
              #6

              @Alexanov

              How did you use the write() function?
              If you used it like this: file.wrte(c) then your file will be 0 byte.
              Because, the c[0]=0 causes your string to contain null characters.
              The QIODevice::write(const char *data) writes data from a zero-terminated string of 8-bit characters to the device.

              A 1 Reply Last reply
              1
              • VRoninV VRonin

                @Alexanov said in Write on File:

                char *c;

                That's a dangling pointer. you can't do anything with it. c[i]=i; messes up your memory, I'm surprised your OS doesn't just crash the app on the spot. on top of that you are overflowing the char type as it can't reach beyond 127.

                QByteArray c;
                for(int i=0; i<5700;++i)
                c.append(QByteArray::number(i));
                file.write(c);
                
                A Offline
                A Offline
                Alexanov
                wrote on last edited by
                #7

                @VRonin

                @VRonin said in Write on File:

                Hi VRonin I did it but How can I access to special position of data in your solution?
                for ex. How can I access to index ="1200" ?(because you append all numbers in ending of previous number )

                QByteArray c;
                for(int i=0; i<5700;++i)
                c.append(QByteArray::number(i));
                file.write(c);
                
                1 Reply Last reply
                0
                • D Devopia53

                  @Alexanov

                  How did you use the write() function?
                  If you used it like this: file.wrte(c) then your file will be 0 byte.
                  Because, the c[0]=0 causes your string to contain null characters.
                  The QIODevice::write(const char *data) writes data from a zero-terminated string of 8-bit characters to the device.

                  A Offline
                  A Offline
                  Alexanov
                  wrote on last edited by
                  #8

                  @Devopia53
                  Hi
                  I knew my problem what was and I want to find solution to do it .
                  Thanks :)

                  mrjjM 1 Reply Last reply
                  0
                  • A Alexanov

                    @Devopia53
                    Hi
                    I knew my problem what was and I want to find solution to do it .
                    Thanks :)

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Alexanov

                    • How can I access to index ="1200"

                    Hi
                    QByteArray can also for do [index]
                    so c[index] also works .

                    http://doc.qt.io/qt-5/qbytearray.html#operator-5b-5d

                    1 Reply Last reply
                    2
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Oh
                      Just a note:
                      QByteArray is empty at first.

                      if you want to use index randomly and not in a loop
                      you must ask it to be if a given size first

                      http://doc.qt.io/qt-5/qbytearray.html#reserve

                      Wrong way:
                      QByteArray c; // empty from start
                      c[1000]=1; // this will crash as it do not have 1000 entries

                      Right way
                      QByteArray c;
                      c.reserve(2000); // make it have size from begining
                      c[1000]=1; // this is ok as it has that index

                      A 1 Reply Last reply
                      2
                      • mrjjM mrjj

                        Oh
                        Just a note:
                        QByteArray is empty at first.

                        if you want to use index randomly and not in a loop
                        you must ask it to be if a given size first

                        http://doc.qt.io/qt-5/qbytearray.html#reserve

                        Wrong way:
                        QByteArray c; // empty from start
                        c[1000]=1; // this will crash as it do not have 1000 entries

                        Right way
                        QByteArray c;
                        c.reserve(2000); // make it have size from begining
                        c[1000]=1; // this is ok as it has that index

                        A Offline
                        A Offline
                        Alexanov
                        wrote on last edited by
                        #11

                        @mrjj
                        Hi Thank you.
                        I stored all data with indexes like this:

                        QByteArray c;
                        for(int i=0; i<5700;++i)
                        c[i]=i;
                        file.write(c)
                        

                        but the main problem is:
                        when i wrote QByteArray in file and Read with Matlab , It seemed all data stored Apart and ASCII value (not number).

                        mrjjM 1 Reply Last reply
                        0
                        • A Alexanov

                          @mrjj
                          Hi Thank you.
                          I stored all data with indexes like this:

                          QByteArray c;
                          for(int i=0; i<5700;++i)
                          c[i]=i;
                          file.write(c)
                          

                          but the main problem is:
                          when i wrote QByteArray in file and Read with Matlab , It seemed all data stored Apart and ASCII value (not number).

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on last edited by mrjj
                          #12

                          @Alexanov
                          What do you mean apart ?

                          you should store a lot of integers in the file.

                          If it looks like text, then maybe you open the file in text mode and not binary ?

                          can you show your code ? ( the real code)

                          can you also show a bit from the file ?

                          A 1 Reply Last reply
                          0
                          • mrjjM mrjj

                            @Alexanov
                            What do you mean apart ?

                            you should store a lot of integers in the file.

                            If it looks like text, then maybe you open the file in text mode and not binary ?

                            can you show your code ? ( the real code)

                            can you also show a bit from the file ?

                            A Offline
                            A Offline
                            Alexanov
                            wrote on last edited by
                            #13

                            @mrjj said in Write on File:

                            What do you mean apart ?

                            Sorry i Mean not apart and all data seem append ;

                            can you show your code ? ( the real code)

                            QString filename = "mydata.bin";
                                QFile myfile(filename);
                                myfile.open(QIODevice::ReadWrite);
                            
                                QByteArray c;
                                for(int i=0 ; i<5700;i++){
                                    c[i]=i;
                            
                                }
                                myfile.write(c);
                                myfile.close();
                            
                            
                            VRoninV 1 Reply Last reply
                            0
                            • A Alexanov

                              @mrjj said in Write on File:

                              What do you mean apart ?

                              Sorry i Mean not apart and all data seem append ;

                              can you show your code ? ( the real code)

                              QString filename = "mydata.bin";
                                  QFile myfile(filename);
                                  myfile.open(QIODevice::ReadWrite);
                              
                                  QByteArray c;
                                  for(int i=0 ; i<5700;i++){
                                      c[i]=i;
                              
                                  }
                                  myfile.write(c);
                                  myfile.close();
                              
                              
                              VRoninV Offline
                              VRoninV Offline
                              VRonin
                              wrote on last edited by VRonin
                              #14

                              to serialise numbers you have to take care of two things:

                              • int in C++ has no defined size. in the vast majority of cases it's 32 bits but in some old or very constrained (embedded devices) systems it might be 16 bits so you need to convert it to a format that all systems will agree on (solution: use qint32)
                              • different systems have different endianness (solution: use QDataStream)

                              Write

                              QString filename = "mydata.bin";
                                  QFile myfile(filename);
                                  myfile.open(QIODevice::WriteOnly);
                              QDataStream fileStream(&myfile);
                              for(qint32 i=0 ; i<5700;i++){
                                      fileStream << i;
                                  }
                               myfile.close();
                              

                              Read

                              QString filename = "mydata.bin";
                                  QFile myfile(filename);
                                  myfile.open(QIODevice::ReadOnly);
                              QDataStream fileStream(&myfile);
                              qint32 value;
                              for(;;){
                              fileStream.startTransaction();
                              fileStream >> value;
                              if(fileStream.commitTransaction())
                              qDebug() << "Read: " <<value;
                              else
                              break;
                              }
                               myfile.close();
                              

                              "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

                              A 1 Reply Last reply
                              2
                              • VRoninV VRonin

                                to serialise numbers you have to take care of two things:

                                • int in C++ has no defined size. in the vast majority of cases it's 32 bits but in some old or very constrained (embedded devices) systems it might be 16 bits so you need to convert it to a format that all systems will agree on (solution: use qint32)
                                • different systems have different endianness (solution: use QDataStream)

                                Write

                                QString filename = "mydata.bin";
                                    QFile myfile(filename);
                                    myfile.open(QIODevice::WriteOnly);
                                QDataStream fileStream(&myfile);
                                for(qint32 i=0 ; i<5700;i++){
                                        fileStream << i;
                                    }
                                 myfile.close();
                                

                                Read

                                QString filename = "mydata.bin";
                                    QFile myfile(filename);
                                    myfile.open(QIODevice::ReadOnly);
                                QDataStream fileStream(&myfile);
                                qint32 value;
                                for(;;){
                                fileStream.startTransaction();
                                fileStream >> value;
                                if(fileStream.commitTransaction())
                                qDebug() << "Read: " <<value;
                                else
                                break;
                                }
                                 myfile.close();
                                
                                A Offline
                                A Offline
                                Alexanov
                                wrote on last edited by
                                #15

                                @VRonin
                                Hi VRonin
                                Thank you :)
                                I said you before, your solution will take time and time is important factor for me, I want to fill my QByteArrays with all data and then Write in file , I do not want to store one-by-one With stream .
                                I could fill QByteArrays and write in file but I can not see true saved Data in Matlab.(for ex. i store number from zero to 5700 in file with QByteArrays like above code but when open it in Matlab i saw wrong size of var and wrong values )

                                VRoninV 1 Reply Last reply
                                0
                                • A Alexanov

                                  @VRonin
                                  Hi VRonin
                                  Thank you :)
                                  I said you before, your solution will take time and time is important factor for me, I want to fill my QByteArrays with all data and then Write in file , I do not want to store one-by-one With stream .
                                  I could fill QByteArrays and write in file but I can not see true saved Data in Matlab.(for ex. i store number from zero to 5700 in file with QByteArrays like above code but when open it in Matlab i saw wrong size of var and wrong values )

                                  VRoninV Offline
                                  VRoninV Offline
                                  VRonin
                                  wrote on last edited by VRonin
                                  #16

                                  The problem is that you didn't really grasp what a QByteArray is.

                                  It's a "vector" of 8bit signed numbers (-128 to +127 each).

                                  if you call c[i]=i; when i > 127 you are saying to take the first 8 bits of the binary representation of i and sticking it into c[i] hence the result is not what you expect.

                                  @Alexanov said in Write on File:

                                  your solution will take time and time is important factor for me, I want to fill my QByteArrays with all data and then Write in file

                                  myfile.write(c); has pretty much the same speed as my code.


                                  P.S.
                                  If you are dealing with matlab import, make sure you take care of the endianness: https://uk.mathworks.com/help/matlab/ref/fread.html#inputarg_machinefmt


                                  Can something like:

                                  QVector<int> intArray(5700);
                                  for(int i=0 ; i<5700;i++){
                                          intArray[i]=i;
                                      }
                                  QString filename = "mydata.bin";
                                      QFile myfile(filename);
                                      myfile.open(QIODevice::ReadWrite);
                                  QDataStream fileStream(&myfile);
                                  for(int singleNum : intArray)
                                  fileStream << singleNum ;
                                  myfile.close();
                                  

                                  work for you? QVector allows random access

                                  "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

                                  A 1 Reply Last reply
                                  2
                                  • VRoninV VRonin

                                    The problem is that you didn't really grasp what a QByteArray is.

                                    It's a "vector" of 8bit signed numbers (-128 to +127 each).

                                    if you call c[i]=i; when i > 127 you are saying to take the first 8 bits of the binary representation of i and sticking it into c[i] hence the result is not what you expect.

                                    @Alexanov said in Write on File:

                                    your solution will take time and time is important factor for me, I want to fill my QByteArrays with all data and then Write in file

                                    myfile.write(c); has pretty much the same speed as my code.


                                    P.S.
                                    If you are dealing with matlab import, make sure you take care of the endianness: https://uk.mathworks.com/help/matlab/ref/fread.html#inputarg_machinefmt


                                    Can something like:

                                    QVector<int> intArray(5700);
                                    for(int i=0 ; i<5700;i++){
                                            intArray[i]=i;
                                        }
                                    QString filename = "mydata.bin";
                                        QFile myfile(filename);
                                        myfile.open(QIODevice::ReadWrite);
                                    QDataStream fileStream(&myfile);
                                    for(int singleNum : intArray)
                                    fileStream << singleNum ;
                                    myfile.close();
                                    

                                    work for you? QVector allows random access

                                    A Offline
                                    A Offline
                                    Alexanov
                                    wrote on last edited by
                                    #17

                                    @VRonin said in Write on File:

                                    The problem is that you didn't really grasp what a QByteArray is

                                    Thanks for your explanation :)
                                    I think in our conversation occurred misunderstanding between me and you or I did not explain my problem very well.
                                    But my problem solved thanks ;)

                                    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