PDA

View Full Version : Reading a file with differences type of data



Alex22
14th January 2016, 16:57
Hi,
There is a file including varies type of data. data is written in this file as below:
1- 500 doubles
2- 500 doubles
3- 500 doubles
4- 500 ints
5- 500 QString inside a QStringList

and the code for writing this file is:


void write()
{
QFile g1f, g2f, of, chf, namef;
double g1[500];
double g2[500];
double ofse[500];
int ch[500];
QStringList names;
for(int i=0; i<500; ++i)
{
g1[i] = i/2.0;
g2[i] = 2.0*i;
ofse[i] = 1;
ch[i]=i;
names.append(QString("name%1").arg(i));
}

g1f.setFileName("oneFile.th");
if(!g1f.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
g1f.write((char*)g1, (500)*sizeof(double));
g1f.flush();
g1f.close();

g2f.setFileName("oneFile.th");
if(!g2f.open(QFile::Append))
{
qDebug()<<"can not open to write";
}
g2f.write((char*)g2, (500)*sizeof(double));
g2f.flush();
g2f.close();


of.setFileName("oneFile.th");
if(!of.open(QFile::Append))
{
qDebug()<<"can not open to write";
}
of.write((char*)ofse, (500)*sizeof(double));
of.flush();
of.close();

chf.setFileName("oneFile.th");
if(!chf.open(QFile::Append))
{
qDebug()<<"can not open to write";
}
chf.write((char*)ch, (500)*sizeof(int));
chf.flush();
chf.close();

namef.setFileName("oneFile.th");
if(!namef.open(QFile::Append))
{
qDebug()<<"can not open to write";
}
QDataStream df(&namef);
df << names;
namef.flush();
namef.close();
}


now i want to read this file and separate to:
1- 500 doubles >>>>> double ge1[500]
2- 500 doubles >>>>> double ge2[500]
3- 500 doubles >>>>> double of[500]
4- 500 ints >>>>> int ch[500]
5- 500 QString inside a QStringList >>>>> QStringList nam

please help me to read and separate this file.

thanks

Lesiok
14th January 2016, 17:10
Exactly the same as you save only use read instead of write.

anda_skoa
14th January 2016, 17:15
And you had read working before, hadn't you?

Cheers,
_

Alex22
14th January 2016, 17:46
Exactly the same as you save only use read instead of write.

But first it must be separated. how can i separate?


And you had read working before, hadn't you?

Cheers,
_

what do you mean anda_skoa?

Lesiok
14th January 2016, 18:37
In the first step You saved to a file memory area with size 500 * sizeof (double) from the array g1. Exactly the same load the file into memory and have data array double [500]. Do not close the file between subsequent readings.

Alex22
14th January 2016, 19:02
In the first step You saved to a file memory area with size 500 * sizeof (double) from the array g1. Exactly the same load the file into memory and have data array double [500]. Do not close the file between subsequent readings.

Thanks. I used this:


void read()
{
QFile g1f, g2f, of, chf, namef;
double g1[500];
double g2[500];
double ofse[500];
int ch[500];
QStringList names;


g1f.setFileName("oneFile.th");
if(!g1f.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
g1f.read((char*)g1, (500)*sizeof(double));


g2f.setFileName("oneFile.th");
if(!g2f.open(QFile::Append))
{
qDebug()<<"can not open to read";
}
g2f.read((char*)g2, (500)*sizeof(double));



of.setFileName("oneFile.th");
if(!of.open(QFile::Append))
{
qDebug()<<"can not open to read";
}
of.read((char*)ofse, (500)*sizeof(double));


chf.setFileName("oneFile.th");
if(!chf.open(QFile::Append))
{
qDebug()<<"can not open to read";
}
chf.read((char*)ch, (500)*sizeof(int));


namef.setFileName("oneFile.th");
if(!namef.open(QFile::Append))
{
qDebug()<<"can not open to write";
}
QDataStream df(&namef);
df >> names;
namef.close();

for(int y=0; y<500; y++)
qDebug()<<ch[y];
}


now i have 2 issues. it gives me this when i want to read:
QIODevice::read (QFile, "oneFile.th"): WriteOnly device
QIODevice::read (QFile, "oneFile.th"): WriteOnly device
QIODevice::read (QFile, "oneFile.th"): WriteOnly device
QIODevice::read (QFile, "oneFile.th"): WriteOnly device

and for example in "int ch[500]", after reading, ch[499] must equal to 499 but it equals to 497! what is wrong?

ChrisW67
14th January 2016, 20:04
When you write the one file use only a single QFile, open file to write, write all the data, close the file.
When you read the one file use only a single QFile, open file to read, read all the data in the same order it was written, close the file.

Alex22
14th January 2016, 20:25
When you write the one file use only a single QFile, open file to write, write all the data, close the file.
When you read the one file use only a single QFile, open file to read, read all the data in the same order it was written, close the file.

Thanks ChrisW67. I used this code:


void write()
{
QFile g1f;
double g1[500];
double g2[500];
double ofse[500];
int ch[500];
QStringList names;
for(int i=0; i<500; ++i)
{
g1[i] = i/2.0;
g2[i] = 2.0*i;
ofse[i] = 1;
ch[i]=i;
names.append(QString("name%1").arg(i));
}

g1f.setFileName("oneFile.th");
if(!g1f.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
g1f.write((char*)g1, (500)*sizeof(double));
g1f.write((char*)g2, (500)*sizeof(double));
g1f.write((char*)ofse, (500)*sizeof(double));
g1f.write((char*)ch, (500)*sizeof(int));
QDataStream df(&g1f);
df << names;
g1f.flush();
g1f.close();
}

void read()
{

QFile g1f;
double g1[500];
double g2[500];
double ofse[500];
int ch[500];
QStringList names;
names.clear();


g1f.setFileName("oneFile.th");
if(!g1f.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
g1f.read((char*)g1, (500)*sizeof(double));
g1f.read((char*)g2, (500)*sizeof(double));
g1f.read((char*)ofse, (500)*sizeof(double));
g1f.read((char*)ch, (500)*sizeof(int));
QDataStream df(&g1f);
df >> names;
g1f.close();

for(int y=0; y<500; y++)
qDebug()<<y<<names;
}


it is well now

ChrisW67
14th January 2016, 20:34
Don't open the file for appending when you write. No need to call flush().
Do open the file for reading before you try to read from it.

You may be better off using QDataStream for all the data and QVector<double>/QVector<int> instead of bare arrays

anda_skoa
14th January 2016, 22:26
what do you mean anda_skoa?
You already had code to read the data.

http://www.qtcentre.org/threads/64764-helping-for-writing-and-reading-with-only-one-file?p=286167#post286167

Did you somehow lose it and forget that you also posted it?

Cheers,
_

Alex22
15th January 2016, 04:29
@anda_skoa, you are right. sorry about that. I could write in one file but i could not read from that file. thanks for mention and sorry again.