Write on File



  • 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?



  • @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.
    


  • @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);
    


  • @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);
    




  • @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.



  • @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);
    


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


  • Qt Champions 2016

    @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


  • Qt Champions 2016

    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



  • @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).


  • Qt Champions 2016

    @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 ?



  • @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();
    
    


  • 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();
    


  • @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 )



  • 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



  • @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 ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.