Shoutbox

Saveing in C++ - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Saveing in C++ (/showthread.php?tid=36341)

Saveing in C++ by DJeX on 01-02-2005 at 09:34 PM

How would I save everything in my program the way it is and the next time I open my program every thing is back the same.


RE: Saveing in C++ by RaceProUK on 01-02-2005 at 10:18 PM

I recommend you look at the Registry API. By creating your own set of keys and values, you can store all your program's settings in one place. It's quite easy to do actually, just be careful of datatypes.


RE: Saveing in C++ by DJeX on 01-02-2005 at 10:59 PM

raceprouk,

I thought of that idea. But I have to much to save to the registry. I need to save it to a file.

Any one have any ideas?


RE: Saveing in C++ by Choli on 01-02-2005 at 11:08 PM

make each class dump all its variables (attributes) to a file (only one file, or one file per class) and next time, create the classes and make them read from the file in the same order you wrote to the file. That's the easyest way (for large amount of data). There are more complex ways to do the same, but all of them rely on the idea of having a file where you store all the info that defines the current status of your program.


RE: Saveing in C++ by DJeX on 01-03-2005 at 01:58 AM

Sorry Choli, I'm just starting out with C++. How would I dump the class attributes to a file?


RE: Saveing in C++ by CookieRevised on 01-03-2005 at 02:58 AM

To store data there are roughly two methods.

One method is to use the registry:
Benefits:
  + no extra file needed
Disadvantages:
  + Can't be easy exported/imported or changed by newbies
  + There are certain (unwritten) rules you must follow; you can't go storing data for your application wherever you like.
  + More and more the registry will get messed up with leftovers from bad written programs (also, the bigger the registry, the slower your PC will start up!)

Another method is to use a file:
Benefits:
  + You can write your own data storage format as you like (binary/textual/whatever).
  + You could use the known INI format to store data
  + Easy exported/imported/changed
  + The user can be certain that whenever he moves/deletes the application, all data and files are moved/deleted with it
Disadvantages:
  + an extra file could be needed (the file to store the data in)*

If you want to stick to an INI-file:
http://www.functionx.com/bcb/howto/inifile.htm
http://www.codeguru.com/Cpp/W-P/files/inifiles/article.php/c4455/

Although many people could say INI-files is something from the past, they are still much prefered and used by many others because of their easyness to create and handle (both by the programmer as by the user); you _know_ what the application changes, etc... Personally I also hate it when I'm never certain of what an application stores/changes what and where in the registry (Also, often things get left in the registry because of errors, etc. making it one big mess).

* PS: why I said "could be needed"? Because you could write a storage system where the actual exe-file is used as a storage file (eg: resources or other data added to the actual file). Of course in that case, the easnyess of using an extra textbased file will be gone.


RE: Saveing in C++ by Choli on 01-03-2005 at 08:51 PM

quote:
Originally posted by DJeX
Sorry Choli, I'm just starting out with C++. How would I dump the class attributes to a file?
the basic idea is that you create a metod that when called if saves all the variables to the file and another one for reading the file and setting the variables with the proper value. If you're new with C/C++, you should get used to manage files (open, close, read, write, .... them).

IMO, you have to ask yourself several questions: do you really want to save the exact status of your program or you only want to save the user settings? Think a bit about that, it's not the same and probably you do not want the first thing * (which is like pausing the execution of the program and resuming it later; quite complex because it envolves more things than just saving and loading all the status and settings)

As a summary of what Cookie said, and saying like a "geleral rule", you should use the registry to store the user settings of the program, ie: how the program behaves; and save into a file (or files) the data that the program manages: what the programs works with (user's data, documments, etc...).


*-> in case you want to save the whole status of your program: ask yourself: what differences the program in two instants of time? what may change from one moment to the next one? All that has to be measured and forms the status of the program. If you dessign the program well, all that will be in (and will be formed by) all the variables and classes of your program together with the current task that the program is doing, which should be doing nothing, because at the point where you save the status your program shouldn't be doing anything else.

--------

I know all this may seem quite complex to understand, because you're new at c++. Don't worry. First of all get used to the language and then start doing more complex things.

Let me suggest you a book: "Thinking in C++". It's a book made of 2 volumes and you can freely download from the internet (google it). I haven't read the whole book, but it seems a quite good book.
RE: Saveing in C++ by DJeX on 01-03-2005 at 09:37 PM

Thanks for your replies. I'll take a look at the sites that Cookie posted when I get a chance.
 
For what Choil said, I really think I would want to save my programs settings to an ini file because I have tones of controls that need to be saved.

For example: What my program does is it highlights the editbox with the number you specify.

Here’s a picture so you can see what I’m talking about (so I don't sound crazy):

[Image: programss.JPG]

Now I want to save the name's the user types in, the numbers in the edit boxes, the color of the edit boxes, the money amount at the top and the starting date at the top. Then also have it reload the exact same way through the settings file I saved all of it to.

Now I know using 160 editboxs and about 20 static text boxes to make up a pretty good chart (I have to say) is stupid, but you have to give me credit for thinking of a way that I knew how to do it (Y)

With that said you should see why I would prefer the ini file better than the registry. :)

*DJeX readys up for the many sugestions and surprised remarks for his dodgy coding lol*

OH and the best part is the program works! (Y):D


I attached it if anyone wants to give it a try. Thanks.


RE: Saveing in C++ by TheBlasphemer on 01-03-2005 at 09:51 PM

Look up the functions:
fwrite, fopen, fread,fclose, and if you find em try to find a list of related functions.

If you're going Windows only, look at CreateFile, CloseHandle, WriteFile, ReadFile :)

Hope that helps,

bye, TB


RE: Saveing in C++ by DJeX on 01-03-2005 at 10:26 PM

The website CookieRevised posted (http://www.functionx.com/bcb/howto/inifile.htm)

Was just what I was looking for. I can get it to save everyting except the color of the edit boxes.

Cookie would you know how?


This is how I write to the ini:

code:
StartUp->WriteString("CtlValues",  "txtPreview",   Edit1->Text);

^ That writes Edit1's text to ini.

I tryed:

code:
StartUp->WriteString("CtlValues",  "txtColor",   Edit1->Color;

^ But it don't write the color of the box it writes a number like -256438
RE: Saveing in C++ by Choli on 01-03-2005 at 10:27 PM

quote:
Originally posted by DJeX
Now I want to save the name's the user types in, the numbers in the edit boxes, the color of the edit boxes, the money amount at the top and the starting date at the top. Then also have it reload the exact same way through the settings file I saved all of it to.
quote:
Originally posted by Choli
and save into a file (or files) the data that the program manages: what the programs works with (user's data, documments, etc...).
you're right. You have to save all that data into a file.

(and note that you are not saving everithing of your program, you are not saving the whole status of the execution of the program, ie: you aren't saving which control has the focus, the position and size of the window, etc..... (you just don't need to) That's why I said i think you didn't want to save _all_ about the program)
RE: Saveing in C++ by CookieRevised on 01-03-2005 at 11:11 PM

quote:
Originally posted by DJeX
^ But it don't write the color of the box it writes a number like -256438

Well, that IS the colornumber though; a color is nothing more then a number...

A colornumber can be written in roughly two ways: signed and unsigned. -256438 is obviously a signed number (it can have a negative sign or a postive sign, while an unsigned number is always positive)

First, what is a color:

A color is made out of different components. The most known is Red Green and Blue. Each of those values can be from 0 to 255. So to represent all these 3 values in 1 number you simply add them together in a binary way: you "concatenate" the Blue value to the Green value which you "concatenate" to the Red value...

In hexadecimal it looks must simplier to explain: a RGB color is a number between 0 and FFFFFF.

the first FF is the Red value, the second FF is the Green value and the third FF is the Blue value (exactly the same as with HTML colorcodes :D) But together they form the hexadecimal number FFFFFF (being absolutely white). Now, when you convert this number to a decimal format you'll get: 16777215 because this comes from R * 2^0 + G * 2^8 + B * 2^16. So an RGB color uses a maximum of 24bits.

Though, the actual color used by windows consists of 4 individual values. so instead of a maximum of FFFFFF (hexadecimal) you can have FFFFFFFF (hexadecimal), or a maximum of 32bits. But the principle is just the same of course...

Signed/Unsigned:

To store numbers, the computer needs some memory locations. But of course using to much memory space isn't very efficient. That is why you have certain types of numbers. eg, a "byte" uses 1 byte, meaning you can use a number from 0 to 255. a "word" uses 2 bytes (the numbers 0 to 65535), a "double word" uses 4 bytes (the numbers 0 to 2^32-1), etc...

These are all positive numbers of course. So the PC needs to have a system where you can store negative numbers as well. This is where the signed/unsigned system comes into place.

With signed numbers you need to offer 1 bit up for the state of the sign. This leaves one bit less for the actually number:
If you take the space of 2 bytes (a "word"), you can have the number from 0 to 65535 (16bits), thus this will always be positive. Now, offer up 1 bit for the sign and you'll be left with only 15bits for the actual number, so now the number can go from -32767 to +32767 (but still there are 65536 possible numbers of course, just as before as it still uses 16bits in total)

So there you have it, the difference between a signed number and an unsigned number. In most programming languages, the signed number system is used by default.

Though, you can easly convert one into the other. eg: to convert a number in the range -32767 to +32767 (thus 16bit) to the range 0 to 65535, you add 32767 to the signed number: -32767 + 32767 (signed range) = 0 (unsigned range)

To conclude:

The same happens with that number you got; it is a 32bits number, signed.
You know that an unsigned 32bits number goes from 0 to 2^32-1, thus from 0 to 4294967295.
(^32 because there are 32bits to be used. And -1 to compensate for the number 0)
So, a signed 32bits number goes from -2^31+1 to +2^31-1, thus from -2147483647 to +2147483647
(^31 instead of ^32 because there is 1 bit used for the sign. And +1/-1 to compensate for the number 0)

So to get your unsigned 32bit number (the true raw colornumber) you must add 2^31+1 to that number:
-256438 + 2^31+1 =
-256438 + 2147483649 =
2147227211

To work with it:

Now, instead of storing the number as a signed number and then calculate the unsigned number from it when you read it again, you could also store it directly as a unsigned number of course. There are functions for that in each programming language, thus including C++

Also, some functions do accept a signed number and will correctly interpret/convert it to the correct value automatically, so you don't need to do anything at all. This can depend on the programming language and/or the type of function you use the number for.



Hope I made some sense... :p
RE: Saveing in C++ by Choli on 01-03-2005 at 11:39 PM

quote:
Originally posted by CookieRevised
Though, the actual color used by windows consists of 4 individual values. so instead of a maximum of FFFFFF (hexadecimal) you can have FFFFFFFF (hexadecimal), or a maximum of 32bits. But the principle is just the same of course...
the 8 extra bits are used for transparency (alpha channel) so at the end you still have 24 bits.
quote:
Originally posted by CookieRevised
go from -32767 to +32767 [...] So, a signed 32bits number goes from -2^31+1 to +2^31-1, thus from -2147483647 to +2147483647
actually signed numbers stored with n bits go from -2^(n-1) to 2^(n-1) - 1, ie: from -32768 to 32767 and from -2147483648 to 2147483647 for 16 and 32 bits ;)
quote:
Originally posted by CookieRevised
Now, instead of storing the number as a signed number and then calculate the unsigned number from it when you read it again, you could also store it directly as a unsigned number of course. There are functions for that in each programming language, thus including C++
I'd use that signed (and maybe negative) number to store the color and wouldn't change/convert it. You don't have to worry about what number it saves, you just know that the same that saves you can read it later and will be a valid number that represents a color (or whatever other thing you want to save).,

RE: Saveing in C++ by DJeX on 01-04-2005 at 01:07 AM

Ok I understand how it gets the number now. Thanks.

But now how do I call that number back into the Color propertie of my edit box.

I used this to save the Color number:

code:
TIniFile *StartUp = new TIniFile("IValues.ini");

StartUp->WriteString("CtlValues",  "txtColor",     Edit1->Color);

delete StartUp;

I tried to load it in with this but I get debug error:

code:
TIniFile *StartUp = new TIniFile("IValues.ini");

Edit1->Color  = StartUp->ReadString("CtlValues",  "txtColor", "");

delete StartUp;

Debug Errors:

quote:
[C++ Warning] save.cpp(30): W8018 Assigning AnsiString to TColor
[C++ Error] save.cpp(30): E2034 Cannot convert 'AnsiString' to 'TColor'

So it looks like to me that the Edit1->Color can't convert the Color number (AnsiString) to Edit1's Color propertie.

How would I get this to work?


*Sorry for all the questions. You all have been a big help. I'm new to this so don't mind me. :)
RE: Saveing in C++ by RaceProUK on 01-04-2005 at 05:56 PM

You need a function that converts from AnsiString to TColor. Look around at MSDN to see what you can find.