Reading CString from the CArchive datastream into QString (QDataSteam)

76 Views Asked by At

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

0

There are 0 best solutions below