Solved File shows contents as Text even when using QDataStream
-
Hi,
I am using QDataStream to input some data into text file. After saving the file,-> when opened with NotePad++ , data in file are in binary format
-> but when opened with NotePad, the data is visible in text format.Can you please help me understand, why in NotePad the data is seen in text format.
The code snippet,
QFile file("C:/Users/user/Documents/Qt_Conole/File.txt"); if(!file.open(QIODevice::WriteOnly)) { qDebug() << "not open"; return 0; } qDebug() << "open"; file.resize(0); QDataStream stream(&file); stream << QString("apple") << QString("Banana") << QString("papaya"); file.close(); file.open(QIODevice::ReadOnly); QStringList list; QString data; while(!stream.atEnd()) { stream >> data; list.append(data); }
Screenshots attached for Notepad++ and notepad.
-
@Vinoth-Rajendran4 said in File shows contents as Text even when using QDataStream:
Can you please help me understand, why in NotePad the data is seen in text format.
I don't understand, the data is clearly binary - note the "spaces" and special chars between different words.
You should really use a hex editor to see the contents of your file - but it is surely no text file.
Regards
-
You're looking for QTextStream and not QDataStream. See QDataStream documentation: "A data stream is a binary stream of encoded information ..."
-
@aha_1980 Thank you for quick reply.
My use-case is ,I don't want other to understand the content of my file. Is there a way to achieve this ??
-
-
Hi
It is exactly the same for both NotePad and NotePad++
However, NotePad++ tried to show all the binary elements whereas
Notepad only shows printable thing it finds.
So if you remove the black NUL from NP++ output
then it looks the same. -
Hi All,
Thanks for helping me understand how QDataStream works. I need to look for encryption as said by @aha_1980 .