QFile and binary writing problem
-
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(); } //------------------
-
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(); } //------------------
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.
-
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.
@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.
-
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.
-
@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.
@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.
-
@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.
@qAlexKo
QDataStream
writing is for another Qt program which usesQDataStream
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 useQFile::write()
. -
@qAlexKo
QDataStream
writing is for another Qt program which usesQDataStream
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 useQFile::write()
.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?
-
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?
-
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?
@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 suppliesQFile
for this. -
C Christian Ehrlicher moved this topic from C++ Gurus on
-
@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 suppliesQFile
for this.@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 ); -
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?
@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)
-
@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)
-
@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 );@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 beint _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. -
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?
@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.