0

I have an issue where I am getting blank when reading data from CString (CArchive) into QString(QDataStream)

I am in the process of converting my code from windows Visual Studio into linux QT. However I have a data file that I saved of from the windows side by using CArchive to save CString. Then on my new code base, I would like to be able to read my old data file but instead it takes QDataStream. My issue is that i was able to read the data from age no problem in the "readData(QDataStream &ar)", but the name and initial came up blank "" (could be gibberish here)

Before                                                    After
File1.h                                             |        File1.h
...                                                 |        ...      
int     age;                                        |        int     age;
CString name;                                       |        QString name;
CString initial;                                    |        QString initial;
int     height; //cm                                |        int     height;
...                                                 |        ...      
                                                    |        
File1.cpp                                           |        File1.cpp
int writeData(CArchive &ar)                         |        int writeData(QDataStream &ar)
{                                                   |        {
...                                                 |        ...
ar.Write(&age, sizeof(age));                        |        ar.writeRawData(&age, sizeof(age));  
ar << name;                                         |        ar << name;
ar << initial;                                      |        ar << initial;
ar.Write(&height, sizeof(height));                  |        ar..writeRawData(&height, sizeof(height));
...                                                 |        ...
}                                                   |        }
                                                    |        
int readData(CArchive &ar)                          |        int readData(QDataStream &ar)
{                                                   |        {
...                                                 |        ...
ar.Read(&age, sizeof(age));                         |        ar.ReadRawData(&age, sizeof(age));
ar << name;                                         |        ar << name;
ar << initial;                                      |        ar << initial;
ar.Read(&height, sizeof(height));                   |        at.ReadRawData(&height, sizeof(height));
...                                                 |        ...
}                                                   |        }

I thought CString and QString is very similar and should be able to use << to read the serialized CString. Should I be using something else? Please advise. THank you in advance!

I tried to change this into ar.ReadRawData(&name, sizeof(name)); //Size of QString, but it doesnt work either

  • Probably CString and QString are very similar, but CArchive and QDataStream are not. – user253751 Feb 24 '23 at 21:16
  • Based on the documentation I think CArchive stores a \n character after the string and nothing before it, but I'm not sure. Does this seem correct? (open the file in a hex editor and have a look) – user253751 Feb 24 '23 at 21:18
  • thank you, i think you are right on CArchive and QDataStream are not the same. I read a lot of bytes out and I think that the first 3? bytes are the length of the string. so ill have to parse that out first....still have to do more investigation though – 0nePunchMan Feb 24 '23 at 23:07
  • 1
    The first 3 bytes contain *two* pieces of information: A type "tag", and—if the type tag indicates a string—the length of the string. The binary file format used by MFC's serialization infrastructure is—in part—documented here: [TN002: Persistent Object Data Format](https://learn.microsoft.com/en-us/cpp/mfc/tn002-persistent-object-data-format). – IInspectable Feb 25 '23 at 10:12
  • I was curious, so I ran the example found here: https://doc.qt.io/qt-6/qdatastream.html#details. The contents of the produced file is: 00 00 00 1A 00 74 00 68 00 65 00 20 00 61 00 6E 00 73 00 77 00 65 00 72 00 20 00 69 00 73 00 00 00 2A So there are some similarities between CArchive and QDataStream, but I wouldn't want to depend on them being identical. This is a problematic way to do this anyway. Have you considered using XML for the conversion instead? – mzimmers Feb 25 '23 at 18:02

0 Answers0