From: Piranha on
My application uses a config.ini file to store some variables, which
it saves at the end and reads at program start, such as the content of
an edit control or the position and size of the window.
It´s a mixture of values and strings, so to make things easier, I´m
converting everything to string and write it as plain txt.
Obviously I needed a separator there, but the strings can contain any
possible character, so I base64_encode() each piece to make sure there
can´t be any dots in the string and then I separate the pieces by
dots.

Over time I´ve added more and more things the program should save in
there and by now all those conversions have become a huge function.
I´m losing overview in between 100s of itoa(), WideCharToMultiByte()
and the always present base64_encode().
Recently I´m even trying to save a few things that can´t be converted
to string so easy, like a COLORREF struct, meaning I have to split up
the struct into its members, and convert all members separate to get
the string.

I´m wondering, isn´t there an easier way, to read and write the data?
I´ve considered making 2 files, a config.ini where I save strings in
txt format and a config.dat where I save the rest binary, but I don´t
like the idea of 2 separate files there.

Long speech short sense, how can I write a mixture of text and binary
data into a file, and how can I read the the file again to split up
the data and convert all pieces back to their types?

I´m not using any kind of MFC or VC or .NET, but only MinGW/GCC++.
From: ScottMcP [MVP] on
On Mar 24, 11:40 am, Piranha <eu_pira...(a)gmx.net> wrote:
> My application uses a config.ini file to store some variables, which
> it saves at the end and reads at program start, such as the content of
> an edit control or the position and size of the window.
> It´s a mixture of values and strings, so to make things easier, I´m
> converting everything to string and write it as plain txt.
> Obviously I needed a separator there, but the strings can contain any
> possible character, so I base64_encode() each piece to make sure there
> can´t be any dots in the string and then I separate the pieces by
> dots.
>
> Over time I´ve added more and more things the program should save in
> there and by now all those conversions have become a huge function.
> I´m losing overview in between 100s of itoa(), WideCharToMultiByte()
> and the always present base64_encode().
> Recently I´m even trying to save a few things that can´t be converted
> to string so easy, like a COLORREF struct, meaning I have to split up
> the struct into its members, and convert all members separate to get
> the string.
>
> I´m wondering, isn´t there an easier way, to read and write the data?
> I´ve considered making 2 files, a config.ini where I save strings in
> txt format and a config.dat where I save the rest binary, but I don´t
> like the idea of 2 separate files there.
>
> Long speech short sense, how can I write a mixture of text and binary
> data into a file, and how can I read the the file again to split up
> the data and convert all pieces back to their types?
>
> I´m not using any kind of MFC or VC or .NET, but only MinGW/GCC++.

You can create a struct or class that contains all of the data members
to be saved. Something like

struct config
{
int structlength;
int x,y,dx,dy;
char text[50];
....
} theconfig;

The whole thing can be written and read in one shot as a binary image.
No conversions necessary. It can grow in the future by adding new
members at the end, as long as you have a length or version field at
the beginning so you will always know which version you are reading
in.


From: Piranha on
On 24 Mrz., 18:00, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
> On Mar 24, 11:40 am, Piranha <eu_pira...(a)gmx.net> wrote:
>
>
>
>
>
> > My application uses a config.ini file to store some variables, which
> > it saves at the end and reads at program start, such as the content of
> > an edit control or the position and size of the window.
> > It´s a mixture of values and strings, so to make things easier, I´m
> > converting everything to string and write it as plain txt.
> > Obviously I needed a separator there, but the strings can contain any
> > possible character, so I base64_encode() each piece to make sure there
> > can´t be any dots in the string and then I separate the pieces by
> > dots.
>
> > Over time I´ve added more and more things the program should save in
> > there and by now all those conversions have become a huge function.
> > I´m losing overview in between 100s of itoa(), WideCharToMultiByte()
> > and the always present base64_encode().
> > Recently I´m even trying to save a few things that can´t be converted
> > to string so easy, like a COLORREF struct, meaning I have to split up
> > the struct into its members, and convert all members separate to get
> > the string.
>
> > I´m wondering, isn´t there an easier way, to read and write the data?
> > I´ve considered making 2 files, a config.ini where I save strings in
> > txt format and a config.dat where I save the rest binary, but I don´t
> > like the idea of 2 separate files there.
>
> > Long speech short sense, how can I write a mixture of text and binary
> > data into a file, and how can I read the the file again to split up
> > the data and convert all pieces back to their types?
>
> > I´m not using any kind of MFC or VC or .NET, but only MinGW/GCC++.
>
> You can create a struct or class that contains all of the data members
> to be saved.  Something like
>
> struct config
> {
> int structlength;
> int x,y,dx,dy;
> char text[50];
> ...
>
> } theconfig;
>
> The whole thing can be written and read in one shot as a binary image.
> No conversions necessary.  It can grow in the future by adding new
> members at the end, as long as you have a length or version field at
> the beginning so you will always know which version you are reading
> in.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

That sounds great, exactly like what I need, but is that safe?
Suppose I have an std::string in the struct, not knowing how long the
string is, would a binary read get that right? or would I have to
convert it to char[], so I have a fixed size for every member of the
struct?
From: ScottMcP [MVP] on
On Mar 24, 1:10 pm, Piranha <eu_pira...(a)gmx.net> wrote:
> On 24 Mrz., 18:00, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
>
>
>
>
>
> > On Mar 24, 11:40 am, Piranha <eu_pira...(a)gmx.net> wrote:
>
> > > My application uses a config.ini file to store some variables, which
> > > it saves at the end and reads at program start, such as the content of
> > > an edit control or the position and size of the window.
> > > It´s a mixture of values and strings, so to make things easier, I´m
> > > converting everything to string and write it as plain txt.
> > > Obviously I needed a separator there, but the strings can contain any
> > > possible character, so I base64_encode() each piece to make sure there
> > > can´t be any dots in the string and then I separate the pieces by
> > > dots.
>
> > > Over time I´ve added more and more things the program should save in
> > > there and by now all those conversions have become a huge function.
> > > I´m losing overview in between 100s of itoa(), WideCharToMultiByte()
> > > and the always present base64_encode().
> > > Recently I´m even trying to save a few things that can´t be converted
> > > to string so easy, like a COLORREF struct, meaning I have to split up
> > > the struct into its members, and convert all members separate to get
> > > the string.
>
> > > I´m wondering, isn´t there an easier way, to read and write the data?
> > > I´ve considered making 2 files, a config.ini where I save strings in
> > > txt format and a config.dat where I save the rest binary, but I don´t
> > > like the idea of 2 separate files there.
>
> > > Long speech short sense, how can I write a mixture of text and binary
> > > data into a file, and how can I read the the file again to split up
> > > the data and convert all pieces back to their types?
>
> > > I´m not using any kind of MFC or VC or .NET, but only MinGW/GCC++.
>
> > You can create a struct or class that contains all of the data members
> > to be saved.  Something like
>
> > struct config
> > {
> > int structlength;
> > int x,y,dx,dy;
> > char text[50];
> > ...
>
> > } theconfig;
>
> > The whole thing can be written and read in one shot as a binary image.
> > No conversions necessary.  It can grow in the future by adding new
> > members at the end, as long as you have a length or version field at
> > the beginning so you will always know which version you are reading
> > in.- Zitierten Text ausblenden -
>
> > - Zitierten Text anzeigen -
>
> That sounds great, exactly like what I need, but is that safe?
> Suppose I have an std::string in the struct, not knowing how long the
> string is, would a binary read get that right? or would I have to
> convert it to char[], so I have a fixed size for every member of the
> struct?- Hide quoted text -
>
> - Show quoted text -

Everything is the struct has to be fixed size and consist of basic
types, not 'smart' objects like std::anything.

From: Piranha on
On 24 Mrz., 18:56, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
> On Mar 24, 1:10 pm, Piranha <eu_pira...(a)gmx.net> wrote:
>
>
>
>
>
> > On 24 Mrz., 18:00, "ScottMcP [MVP]" <scott...(a)mvps.org> wrote:
>
> > > On Mar 24, 11:40 am, Piranha <eu_pira...(a)gmx.net> wrote:
>
> > > > My application uses a config.ini file to store some variables, which
> > > > it saves at the end and reads at program start, such as the content of
> > > > an edit control or the position and size of the window.
> > > > It´s a mixture of values and strings, so to make things easier, I´m
> > > > converting everything to string and write it as plain txt.
> > > > Obviously I needed a separator there, but the strings can contain any
> > > > possible character, so I base64_encode() each piece to make sure there
> > > > can´t be any dots in the string and then I separate the pieces by
> > > > dots.
>
> > > > Over time I´ve added more and more things the program should save in
> > > > there and by now all those conversions have become a huge function.
> > > > I´m losing overview in between 100s of itoa(), WideCharToMultiByte()
> > > > and the always present base64_encode().
> > > > Recently I´m even trying to save a few things that can´t be converted
> > > > to string so easy, like a COLORREF struct, meaning I have to split up
> > > > the struct into its members, and convert all members separate to get
> > > > the string.
>
> > > > I´m wondering, isn´t there an easier way, to read and write the data?
> > > > I´ve considered making 2 files, a config.ini where I save strings in
> > > > txt format and a config.dat where I save the rest binary, but I don´t
> > > > like the idea of 2 separate files there.
>
> > > > Long speech short sense, how can I write a mixture of text and binary
> > > > data into a file, and how can I read the the file again to split up
> > > > the data and convert all pieces back to their types?
>
> > > > I´m not using any kind of MFC or VC or .NET, but only MinGW/GCC++..
>
> > > You can create a struct or class that contains all of the data members
> > > to be saved.  Something like
>
> > > struct config
> > > {
> > > int structlength;
> > > int x,y,dx,dy;
> > > char text[50];
> > > ...
>
> > > } theconfig;
>
> > > The whole thing can be written and read in one shot as a binary image..
> > > No conversions necessary.  It can grow in the future by adding new
> > > members at the end, as long as you have a length or version field at
> > > the beginning so you will always know which version you are reading
> > > in.- Zitierten Text ausblenden -
>
> > > - Zitierten Text anzeigen -
>
> > That sounds great, exactly like what I need, but is that safe?
> > Suppose I have an std::string in the struct, not knowing how long the
> > string is, would a binary read get that right? or would I have to
> > convert it to char[], so I have a fixed size for every member of the
> > struct?- Hide quoted text -
>
> > - Show quoted text -
>
> Everything is the struct has to be fixed size and consist of basic
> types, not 'smart' objects like std::anything.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Thanks for the help, I believe I´ll be able to work it out now.