structures to a file (read and write)
Guys,
Sample I have structure
Code:
struct s_mystruct
{
int noOfCars;
double noOfWives;
bool isActive;
}mystruct;
mystruct st;
st.noOfCars = 10;
st.noOfWives = 2.5;
st.isActive = true;
I want to write this to a file (text format ) like this
Quote:
NoOfCars = 1
noOfWives = 2.5
isActive = true
Then, read the txt file and fill my structure again.
Question:
Is there any easy way to do this taking advantage of some "QtClasses" ?
baray98
Re: structures to a file (read and write)
Take a look at QSettings:
Code:
// write
settings.setValue("NoOfCars", st.noOfCars);
...
// read
st.noOfCars = settings.value("NoOfCars").toInt();
...
Re: structures to a file (read and write)
thanks for the reply ...just a follow up .. I have encountered the following
from my practice I did the following
Code:
typedef struct s_mystruct
{
int car;
double wife;
bool isActive;
}t_mystruct;
t_mystruct st;
for (int i = 0; i <20;i++) //just to try
{
st.
map.
insert(i,
QString("Driver No. %1").
arg(i
));
}
settings.beginGroup("CarMap");
settings.beginWriteArray("Car");
QList<int> keys = st.map.keys();
for (int i = 0; i < keys.count(); ++i) {
settings.setArrayIndex(i);
int xx = keys.at(i);
settings.setValue("PlateNumber", keys.at(i));
settings.setValue("Driver",st.map.value(keys.at(i)));
}
settings.endArray();
settings.endGroup();
then, i got
Quote:
[CarMap]
Car\1\Driver=Driver No. 0
Car\1\PlateNumber=0
Car\10\Driver=Driver No. 9
Car\10\PlateNumber=9
Car\11\Driver=Driver No. 10
Car\11\PlateNumber=10
Car\12\Driver=Driver No. 11
Car\12\PlateNumber=11
Car\13\Driver=Driver No. 12
Car\13\PlateNumber=12
Car\14\Driver=Driver No. 13
Car\14\PlateNumber=13
Car\15\Driver=Driver No. 14
Car\15\PlateNumber=14
Car\16\Driver=Driver No. 15
Car\16\PlateNumber=15
Car\17\Driver=Driver No. 16
Car\17\PlateNumber=16
Car\18\Driver=Driver No. 17
Car\18\PlateNumber=17
Car\19\Driver=Driver No. 18
Car\19\PlateNumber=18
Car\2\Driver=Driver No. 1
Car\2\PlateNumber=1
Car\20\Driver=Driver No. 19
Car\20\PlateNumber=19
Car\3\Driver=Driver No. 2
Car\3\PlateNumber=2
Car\4\Driver=Driver No. 3
Car\4\PlateNumber=3
Car\5\Driver=Driver No. 4
Car\5\PlateNumber=4
Car\6\Driver=Driver No. 5
Car\6\PlateNumber=5
Car\7\Driver=Driver No. 6
Car\7\PlateNumber=6
Car\8\Driver=Driver No. 7
Car\8\PlateNumber=7
Car\9\Driver=Driver No. 8
Car\9\PlateNumber=8
Car\size=20
How can I changed the sorting style of the ouput text , I want that all cars should be in order, not like above?
ie Car\10\... should follow Car\9\...
baray98
Re: structures to a file (read and write)
What for? The order doesn't really matter, it's just a config file... If you want to change the order, prepend all single digit section names with "0". Of course you won't be able to use automatic section naming through array support.
Re: structures to a file (read and write)
So Ill take it as a NO ...can't do it
and for your question of why......the answer is below
I may need to edit this manually in the future and i dont want to be confused of how many cars do i have ?
Re: structures to a file (read and write)
Quote:
Originally Posted by
baray98
I may need to edit this manually in the future and i dont want to be confused of how many cars do i have ?