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, 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. RE: Saveing in C++ by Choli on 01-03-2005 at 08:51 PM
quote: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. RE: Saveing in C++ by TheBlasphemer on 01-03-2005 at 09:51 PM
Look up the functions: 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) code: ^ That writes Edit1's text to ini. I tryed: code: ^ 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: quote: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: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 ) 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... RE: Saveing in C++ by Choli on 01-03-2005 at 11:39 PM
quote:the 8 extra bits are used for transparency (alpha channel) so at the end you still have 24 bits. quote: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: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. code: I tried to load it in with this but I get debug error: code: Debug Errors: quote: 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. |