Reputation: 171
I work on project that involved writing binary data, I use QString for storing line and then save it on hard by Qfile
data.append(QChar(10));
and I set encoding to "ISO-8859-1", anyway when the data equal to 10 I as showed above, the saved data is 0x0d,0x0a I find the problem source ,10='\n' and Qt change it to '\n\r' so it happened.
How can I fix it?
Upvotes: 0
Views: 1531
Reputation: 4412
Your question and answer are poor for several reasons. Here are the problems I've spotted immediately:
data
object is in your question, which means we don't really know what data.append(...)
is doing. Make sure your posts include a short, self contained, correct (compilable), example. Furthermore, in the answer you posted, there is no mention of this data
object.byte
? It's not part of C++ or Qt and those are the only two tags you've given us to go on.Upvotes: 1
Reputation: 171
I found the reason, because the QFile object that I had created is in Text mode so when QFile want to write file it changes /n to /r/n the code below is the previous code that have problem in binary because of existence of QIODevice::Text
QFile myfile;
myfile.setFileName("build/Data.bin");
if(!myfile.open(QIODevice::WriteOnly|QIODevice::Text))
return;
for binary mode simply we can omite QIODevice::Text
so the second line change to
if(!myfile.open(QIODevice::WriteOnly))
at first I think this is the Qt's bug, and try to use ofstream to write file, the same thing happened with ofstream, and finally writing binary code with ofstream is showed below
the point is std::ios_base::binary
ofstream file;
file.open ("example.txt", std::ios_base::out | std::ios_base::binary);
file << (byte)10;
file.close();
Upvotes: 0