PDA

View Full Version : helping for writing and reading with only one file



Alex22
5th January 2016, 14:35
Hi,
Because of needing for saving some double, int and QStringList variables, my project works with 3 files that have written and read binary. these 3 files are for reading from them and setting some values from them and if there are some changing, I save (write) the changes in these files. I compiled my project statically and all of output is a standalone ".exe" and these 3 files beside of this ".exe".
I want to put these 3 files inside the ".exe" but because of writing into them I can not put them into the resources folder.
one idea is: writing all of my data (double, int and QStringList variables) only into the 1 file. please help me in this way how could I.
and in the other hand, is it possible to put these 3 files into ".exe" of my project?

my code:



QFile g1f, g2f, of, namef // writing my 3 files
g1f.setFileName("ga.bin");
if(!g1f.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
g1f.write((char*)g1, 500*sizeof(double));//1-writing binary my double variables into a file in name of "ga.bin"
g1f.flush();
g1f.close();

g2f.setFileName("g.bin");
if(!g2f.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
g2f.write((char*)g2, 500**sizeof(int));//2-writing binary my int variables into an another file in name of "g.bin"
g2f.flush();
g2f.close();

namef.setFileName("n.bin");//3--writing binary my QString variables into an another file in name of "n.bin"
if(!namef.open(QFile::WriteOnly))
{
qDebug()<<"can not open to write";
}
QDataStream df(&namef);
df << names;//names is an object of QStringList
namef.flush();
namef.close();






QFile g1f1, g2f1, of1, namef1 //reading 3 files that has been written above

g1f1.setFileName("ga.bin");//1-reading binary the double variables
if(!g1f1.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
g1f1.read((char*)g1, 500**sizeof(double));
g1f1.close();

g2f1.setFileName("g.bin");
if(!g2f1.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
g2f1.read((char*)g2, 500*sizeof(int));//2-reading binary the int variables
g2f1.close();


of1.setFileName("o.bin");
if(!of1.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
of1.read((char*)ofse, 500*sizeof(double));
of1.close();

namef1.setFileName("n.bin");//3-reading binary the QString variables
if(!namef1.open(QFile::ReadOnly))
{
qDebug()<<"can not open to read";
}
QDataStream fg(&namef1);
fg >> names1;//names1 is an object of QStringList
close();



first, can i put these 3 files (that i am reading from them and writing into them), into the ".exe" file and not beside of it? if not, then how can i have only one file instead of 3 files?

thanks for any help

d_stranz
5th January 2016, 14:46
Windows will not allow you to change an EXE file while it is running, so you cannot add new text to the same file as your EXE. You have to use a separate file.

I don't understand why you think you need 3 files. If the content is always 500 doubles and ints, then just write the doubles and then the ints to the same file. If you want to use QDataStream to read and write the strings, then use QDataStream to read and write the arrays of doubles and ints also.

anda_skoa
5th January 2016, 14:57
If the program needs a file for input on startup, you could do this:

1) check if the file exists in the user's data location
2) if not, extract from resource to the user's data location
3) load file from the user's data location

Writing can then always happen to that file.

Cheers,
_

Alex22
5th January 2016, 15:13
If the content is always 500 doubles and ints, then just write the doubles and then the ints to the same file. If you want to use QDataStream to read and write the strings, then use QDataStream to read and write the arrays of doubles and ints also.

Thanks for your answering. Yes the content is always 500 "doubles" and "ints" and 500 names in a "QStringList" (that length of each name is not constant and certain). please help me to write/read only one file in this way. thanks a lot

anda_skoa
5th January 2016, 15:17
Simple make the write() and read() calls on the same file?

Cheers,
_

Alex22
5th January 2016, 15:26
If the program needs a file for input on startup, you could do this:

1) check if the file exists in the user's data location
2) if not, extract from resource to the user's data location
3) load file from the user's data location

Writing can then always happen to that file.

Cheers,
_

Really thanks, nice way. but i have some questions:
- how could extract from resource? (by this i could read/write and files are not beside of the ".exe")
- is it possible to load from the user's data location to resource?

anda_skoa
5th January 2016, 16:16
- how could extract from resource? (by this i could read/write and files are not beside of the ".exe")

QFIle::copy() should work, otherwise readAll() on a QFile that has the resource path and writing that into the QFile at the data location.



- is it possible to load from the user's data location to resource?
Not sure what you mean.
QFile based loading is transparent on whether the path is a resource or a local file.

With the described "extract" approach you would never read from the resource directly, always from the local file.

Cheers,
_