Writing bytes to a file



  • Hello everybody.

    Here is my code:

    	QString filename = m_outputfolder;
    	filename = filename.append("/");
    	filename = filename.append(m_database->m_reference);
    	filename = filename.append(ext_output);
    	QFile file(filename);
    	file.open(QIODevice::WriteOnly);
    	QDataStream out(&file);
    	out << header;
    	out << msg_out.toUtf8();
    	out << m_database->m_operator.toUtf8();
    	out << separator;
    	out << m_database->m_operatorDates.toUtf8();
    	out << separator;
    	out << m_database->m_period.toUtf8();
    	out << separator;
    	out << m_database->m_duration.toUtf8();
    	file.close();
    

    header is defined :

    static const qint8 header = (qint8) 0xF6;
    

    separator :

    static const qint8 separator = (qint8) 0x09;
    

    The trouble comes with the file because I get extra bytes, always 4 bytes that I do not want.
    Here is the file:

    f6 00 00 00 09 ....... (for msg_out) 00 00 00 0e ....... (for m_operator) 09 00 00 00 17 ..... (for m_operatorDates) 09
    00 00 00 09 ...... (for m_period) 09 00 00 00 08 ......(for m_duration) END OF FILE

    As you can see extra bytes are written, always a group of 4 bytes.
    Could you help please ?
    Thanks


  • Moderators

    Hi! The stream gets encoded to allow platform independence. What the actual binary representation looks like depends on the used data serialization format. If you don't want this you can also read/write raw unencoded binary data.



  • OK. Is it possible the 4 bytes while using << ?



  • I mean remove the 4 bytes


  • Moderators



  • I tried to write a QString using writeRawData.
    I wrote:

    QString stow = stringToWrite.toUtf8();
    const char* ctow = stow.toStdString().c_str();
    ...
    out.writeRawData(ctow, stow.length());
    

    The trouble is that, in the file, the first byte is wrong. It is set to 00.



  • When reading or writing binary files I find it a lot easier to deal with the data directly in a QByteArray. I have used QDataStream in some cases but most of the time the files you are trying to read are foreign to Qt and could be formatted in a variety of ways.

    QByteArray file_data;
    file_data = file.readAll();
    file.close();
    // extract the information needed
    

    The only part about this is when you need to deal with values (integer, double, variable sized strings, ...). I wrote a utility class that allows me to extract or place the value within the file data directly. For example, a 32 bit integer that is little endian will requires 4 bytes so a function is needed that takes an index and the integer value and it handles placing it properly into the QByteArray. When done it is easy to write:

    QByteArray file_data;
    // populate the file_data as needed
    file_data = file.write(file_data);
    file.close();
    

    The one thing about binary files is usually they are well contained and predicable particular if it contains numerical data (if all the values are double precision then they are aligned to 8 byte boundaries). Binary files containing mostly strings are a little harder as you end up walking through the data from start to finish (which you end up doing anyway with a QDataStream).

    Just a thought.



  • I succeeded in using toUtf8() applied to the QString.It gives a QByteArray and then apply data() to get the const char*.


Log in to reply
 

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