PDA

View Full Version : problem with binary writting a QString array



Alex22
19th December 2015, 18:00
hi
i write a QString array in a file in binary mode and then read it from the file. the code is below. for what is it read wrong? and is QString suitable for saving these names?


QString names[125];
for(int i=0; i<125; i++)
{
names[i] = QString("Name%1").arg(i+1);//is QString suitable for saving these names?
}
QFile namef1;
namef1.setFileName("names.bin");
if(!namef1.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
namef1.read((char*)names, 125*sizeof(QString));
namef1.close();





QFile namef;
namef.setFileName("names.bin");
if(!namef.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
namef.write((char*)names, 125*sizeof(QString));
namef.flush();
namef.close();


thanks for any help

Lesiok
19th December 2015, 19:03
First : sizeof(QString) is NOT string length, this is a size of object. Code for writing should be :

for( int i = 0; i <125; i++ )
namef.write(names.toAscii());
Second : strings have different lengths. If you save them without any separator how you split them later?

Alex22
19th December 2015, 19:14
thanks for your answering. you are right. then i must write in text form
is there any example for writing a QString array (that names change in a loop) in text form? and how can i read so? is QStringList better for this?
thanks for more helps

anda_skoa
20th December 2015, 11:04
If you only need this between instances of your program or between Qt programs, then an easy solution is to use QDataStream to serialize QStrings individually or as a QStringList.

If this needs to be read by a non-Qt program and you are in charge of the format, then I would suggest something like a length field followed by UTF-8 data.

Cheers,
_

Alex22
20th December 2015, 16:25
@anda thanks. please help me for writting under code:


QStringList list;
QString names[125];
for(int i=0; i<125; i++)
{
names[i] = QString("Name%1").arg(i+1);
}
QFile namef1("c:/text.txt");
if(!namef1.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
QTextStream out(&namef1);
out << names[i]//how can i first put all names[125] into the "list" and then write it?



QFile namef("c:/text.txt");
if(!namef.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
QTextStream in(&namef);
in>>list;// please help me also to read from file and put it into "list" and after that
//split list to 125 QStrings in names[125]

Lesiok
20th December 2015, 16:52
Simply : forget about the table names.

QStringList list;
for(int i=0; i<125; i++)
list.append(QString("Name%1").arg(i+1));

QFile namef1("c:/text.txt");
if(!namef1.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
QTextStream out(&namef1);
out << list;Work with QStringList not with table.

anda_skoa
20th December 2015, 16:53
QStringList list;
....
QDataStream stream(&file);
stream << list;




QStringList list;
....
QDataStream stream(&file);
stream >> list;


Cheers,
_

Alex22
20th December 2015, 17:18
@anda thanks but how i can put all names[125] (in a for loop) into the list while writting in a file. and how i can get these names from list while reading?

anda_skoa
20th December 2015, 18:10
There is no point in creating the array in the first place.
Just put the strings into the list.

After reading, just work with the list.

Cheers,
_