Thread: [Dev-C++] Loading variables from files [C++]
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Samuel H. <tal...@gm...> - 2008-04-28 05:40:22
|
Hi, I am attempting to load variables from files but im not sure what todo because the variables are different types and sizes. Here is the source: #pragma once #include <fstream> using namespace std; class hero { public: void setname(string first, string last) { fname = first; lname = last; return; } void setmap(int mp) { mapid = mp; return; } void setpos(int x, int y) { posx = x; posy = y; return; } void setmapx(int x) { posx = x; return; } void setmapy(int y) { posy = y; return; } int getmap() { return mapid; } int getmapx() { return posx; } int getmapy() { return posy; } void loadinfo() { // This is where I will load the config. } private: // Map variables. int mapid; int posx; int posy; }; Yours Truly Samuel Hulley. |
From: Per W. <pw...@ia...> - 2008-04-28 08:58:04
|
Decide if you want to use a binary file, or an ASCII file. If you go for text, you can encode: name="hello" map=2 pos=<1,2> ... and then char buf[1000]; char *p,*name; while (fgets(buf,sizeof(buf),f)) { p = strip_white(buf); if (p == '\n') continue; name = get_name(&p); if (name) { p = strip_white(p); if (p != '=') { complain(); return -1; } p = strip_white(++p); if (!strcmp(name,"name")) { if (!get_string(p,my_string,sizeof(my_string))) return -1; } else if (!strcmp(name,"map")) { if (!get_integer(p,0,10)) return -1; } else if (!strcmp(name,"pos")) { if (!get_position(p,&pos)) return -1; } else { complain(); return -1; } } } xx.setname(name); xx.setpos(pos); ... /pwm On Mon, 28 Apr 2008, Samuel Hulley wrote: > Hi, > I am attempting to load variables from files but im not sure what > todo because the variables are different types and sizes. > Here is the source: > > #pragma once > #include <fstream> > using namespace std; > > class hero > { > public: > void setname(string first, string last) > { > fname = first; > lname = last; > return; > } > > void setmap(int mp) > { > mapid = mp; > return; > } > > void setpos(int x, int y) > { > posx = x; > posy = y; > return; > } > > void setmapx(int x) > { > posx = x; > return; > } > > void setmapy(int y) > { > posy = y; > return; > } > > int getmap() > { > return mapid; > } > > int getmapx() > { > return posx; > } > > int getmapy() > { > return posy; > } > > void loadinfo() > { > // This is where I will load the config. > } > > private: > // Map variables. > int mapid; > int posx; > int posy; > }; > > Yours Truly > Samuel Hulley. > > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > Don't miss this year's exciting event. There's still time to save $100. > Use priority code J8TL2D2. > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Derek C. <de...@ci...> - 2008-04-28 10:08:30
|
A good way is to add serialise operators to the class: ostream& hero::operator>>(ostream& stream) { mapid >> stream; posx >> stream; posy >> stream; return stream; } istream& hero::operator <<(istream& stream) { mapid << stream: posx << stream; posy << stream; return stream; } On Mon, Apr 28, 2008 at 6:40 AM, Samuel Hulley <tal...@gm...> wrote: > Hi, > I am attempting to load variables from files but im not sure what > todo because the variables are different types and sizes. > Here is the source: > > #pragma once > #include <fstream> > using namespace std; > > class hero > { > public: > void setname(string first, string last) > { > fname = first; > lname = last; > return; > } > > void setmap(int mp) > { > mapid = mp; > return; > } > > void setpos(int x, int y) > { > posx = x; > posy = y; > return; > } > > void setmapx(int x) > { > posx = x; > return; > } > > void setmapy(int y) > { > posy = y; > return; > } > > int getmap() > { > return mapid; > } > > int getmapx() > { > return posx; > } > > int getmapy() > { > return posy; > } > > void loadinfo() > { > // This is where I will load the config. > } > > private: > // Map variables. > int mapid; > int posx; > int posy; > }; > > Yours Truly > Samuel Hulley. > > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > Don't miss this year's exciting event. There's still time to save $100. > Use priority code J8TL2D2. > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Per W. <pw...@ia...> - 2008-04-28 16:31:42
|
But remember that you may quite often want to make the serialize a lot more advanced, with versioning information. Serialize is best when the same version of an application on the same architecture will load a saved state. Life may quickly get complicated if moving between different architectures. If you write a 32-bit int and the other program expects an int to be 64 bits large, the serialize will fail. Another thing is when a new version have added more state information in the class. It is quite probable that the end user expects a new version of an application to be able to continue using the existing state information. I often make the serialize methods use text format (together with a bit of glue helper functions) to make the serialize a bit more forgiving. It is also possible to use binary formats. It's just a question about how to handle potential overflows, and to make sure that missing/invalid fields gets reasonable defaults. /pwm On Mon, 28 Apr 2008, Derek Clarke wrote: > A good way is to add serialise operators to the class: > > ostream& hero::operator>>(ostream& stream) > { > mapid >> stream; > posx >> stream; > posy >> stream; > return stream; > } > > istream& hero::operator <<(istream& stream) > { > mapid << stream: > posx << stream; > posy << stream; > return stream; > } > > On Mon, Apr 28, 2008 at 6:40 AM, Samuel Hulley <tal...@gm...> wrote: > > Hi, > > I am attempting to load variables from files but im not sure what > > todo because the variables are different types and sizes. > > Here is the source: > > > > #pragma once > > #include <fstream> > > using namespace std; > > > > class hero > > { > > public: > > void setname(string first, string last) > > { > > fname = first; > > lname = last; > > return; > > } > > > > void setmap(int mp) > > { > > mapid = mp; > > return; > > } > > > > void setpos(int x, int y) > > { > > posx = x; > > posy = y; > > return; > > } > > > > void setmapx(int x) > > { > > posx = x; > > return; > > } > > > > void setmapy(int y) > > { > > posy = y; > > return; > > } > > > > int getmap() > > { > > return mapid; > > } > > > > int getmapx() > > { > > return posx; > > } > > > > int getmapy() > > { > > return posy; > > } > > > > void loadinfo() > > { > > // This is where I will load the config. > > } > > > > private: > > // Map variables. > > int mapid; > > int posx; > > int posy; > > }; > > > > Yours Truly > > Samuel Hulley. > > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > Don't miss this year's exciting event. There's still time to save $100. > > Use priority code J8TL2D2. > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > ------------------------------------------------------------------------- > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > Don't miss this year's exciting event. There's still time to save $100. > Use priority code J8TL2D2. > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
From: Derek C. <de...@ci...> - 2008-04-29 08:18:13
|
Absolutely. I just communicated the essence of the concept and left making it bombproof as an exercise for the reader :-) On Mon, Apr 28, 2008 at 5:31 PM, Per Westermark <pw...@ia...> wrote: > But remember that you may quite often want to make the serialize a lot > more advanced, with versioning information. > > Serialize is best when the same version of an application on the same > architecture will load a saved state. > > Life may quickly get complicated if moving between different > architectures. If you write a 32-bit int and the other program expects > an int to be 64 bits large, the serialize will fail. > > Another thing is when a new version have added more state information in > the class. It is quite probable that the end user expects a new version of > an application to be able to continue using the existing state > information. > > I often make the serialize methods use text format (together with a bit of > glue helper functions) to make the serialize a bit more forgiving. It is > also possible to use binary formats. It's just a question about how to > handle potential overflows, and to make sure that missing/invalid fields > gets reasonable defaults. > > /pwm > > > > On Mon, 28 Apr 2008, Derek Clarke wrote: > > > A good way is to add serialise operators to the class: > > > > ostream& hero::operator>>(ostream& stream) > > { > > mapid >> stream; > > posx >> stream; > > posy >> stream; > > return stream; > > } > > > > istream& hero::operator <<(istream& stream) > > { > > mapid << stream: > > posx << stream; > > posy << stream; > > return stream; > > } > > > > On Mon, Apr 28, 2008 at 6:40 AM, Samuel Hulley <tal...@gm...> wrote: > > > Hi, > > > I am attempting to load variables from files but im not sure what > > > todo because the variables are different types and sizes. > > > Here is the source: > > > > > > #pragma once > > > #include <fstream> > > > using namespace std; > > > > > > class hero > > > { > > > public: > > > void setname(string first, string last) > > > { > > > fname = first; > > > lname = last; > > > return; > > > } > > > > > > void setmap(int mp) > > > { > > > mapid = mp; > > > return; > > > } > > > > > > void setpos(int x, int y) > > > { > > > posx = x; > > > posy = y; > > > return; > > > } > > > > > > void setmapx(int x) > > > { > > > posx = x; > > > return; > > > } > > > > > > void setmapy(int y) > > > { > > > posy = y; > > > return; > > > } > > > > > > int getmap() > > > { > > > return mapid; > > > } > > > > > > int getmapx() > > > { > > > return posx; > > > } > > > > > > int getmapy() > > > { > > > return posy; > > > } > > > > > > void loadinfo() > > > { > > > // This is where I will load the config. > > > } > > > > > > private: > > > // Map variables. > > > int mapid; > > > int posx; > > > int posy; > > > }; > > > > > > Yours Truly > > > Samuel Hulley. > > > > > > ------------------------------------------------------------------------- > > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > > Don't miss this year's exciting event. There's still time to save $100. > > > Use priority code J8TL2D2. > > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > > _______________________________________________ > > > Dev-cpp-users mailing list > > > Dev...@li... > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > Don't miss this year's exciting event. There's still time to save $100. > > Use priority code J8TL2D2. > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > _______________________________________________ > > Dev-cpp-users mailing list > > Dev...@li... > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > |
From: Derek C. <de...@ci...> - 2008-04-29 08:24:38
|
Although I'd put the fancy stuff in my own stream implementations rather than the object serialise functions. That way you don't spread platform specific code around dozens of classes, at the possible cost of a little robustness. On Tue, Apr 29, 2008 at 9:18 AM, Derek Clarke <de...@ci...> wrote: > Absolutely. > > I just communicated the essence of the concept and left making it > bombproof as an exercise for the reader :-) > > > > On Mon, Apr 28, 2008 at 5:31 PM, Per Westermark <pw...@ia...> wrote: > > But remember that you may quite often want to make the serialize a lot > > more advanced, with versioning information. > > > > Serialize is best when the same version of an application on the same > > architecture will load a saved state. > > > > Life may quickly get complicated if moving between different > > architectures. If you write a 32-bit int and the other program expects > > an int to be 64 bits large, the serialize will fail. > > > > Another thing is when a new version have added more state information in > > the class. It is quite probable that the end user expects a new version of > > an application to be able to continue using the existing state > > information. > > > > I often make the serialize methods use text format (together with a bit of > > glue helper functions) to make the serialize a bit more forgiving. It is > > also possible to use binary formats. It's just a question about how to > > handle potential overflows, and to make sure that missing/invalid fields > > gets reasonable defaults. > > > > /pwm > > > > > > > > On Mon, 28 Apr 2008, Derek Clarke wrote: > > > > > A good way is to add serialise operators to the class: > > > > > > ostream& hero::operator>>(ostream& stream) > > > { > > > mapid >> stream; > > > posx >> stream; > > > posy >> stream; > > > return stream; > > > } > > > > > > istream& hero::operator <<(istream& stream) > > > { > > > mapid << stream: > > > posx << stream; > > > posy << stream; > > > return stream; > > > } > > > > > > On Mon, Apr 28, 2008 at 6:40 AM, Samuel Hulley <tal...@gm...> wrote: > > > > Hi, > > > > I am attempting to load variables from files but im not sure what > > > > todo because the variables are different types and sizes. > > > > Here is the source: > > > > > > > > #pragma once > > > > #include <fstream> > > > > using namespace std; > > > > > > > > class hero > > > > { > > > > public: > > > > void setname(string first, string last) > > > > { > > > > fname = first; > > > > lname = last; > > > > return; > > > > } > > > > > > > > void setmap(int mp) > > > > { > > > > mapid = mp; > > > > return; > > > > } > > > > > > > > void setpos(int x, int y) > > > > { > > > > posx = x; > > > > posy = y; > > > > return; > > > > } > > > > > > > > void setmapx(int x) > > > > { > > > > posx = x; > > > > return; > > > > } > > > > > > > > void setmapy(int y) > > > > { > > > > posy = y; > > > > return; > > > > } > > > > > > > > int getmap() > > > > { > > > > return mapid; > > > > } > > > > > > > > int getmapx() > > > > { > > > > return posx; > > > > } > > > > > > > > int getmapy() > > > > { > > > > return posy; > > > > } > > > > > > > > void loadinfo() > > > > { > > > > // This is where I will load the config. > > > > } > > > > > > > > private: > > > > // Map variables. > > > > int mapid; > > > > int posx; > > > > int posy; > > > > }; > > > > > > > > Yours Truly > > > > Samuel Hulley. > > > > > > > > ------------------------------------------------------------------------- > > > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > > > Don't miss this year's exciting event. There's still time to save $100. > > > > Use priority code J8TL2D2. > > > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > > > _______________________________________________ > > > > Dev-cpp-users mailing list > > > > Dev...@li... > > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > > ------------------------------------------------------------------------- > > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > > Don't miss this year's exciting event. There's still time to save $100. > > > Use priority code J8TL2D2. > > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > > _______________________________________________ > > > Dev-cpp-users mailing list > > > Dev...@li... > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > |
From: Per W. <pw...@ia...> - 2008-04-29 09:28:07
|
Remember that the stream can't do everything. It doesn't know what fields it is serializing, so it can't handle fields that are missing or having a changed definition between two versions of a class. The stream can handle endian differences, but do not know what it means for an object if it changes between 32 and 64 bits, or if it is safe to truncate the high 32 bits if loading a 64-bit value into a 32-bit field. But of course all requied helper functions should be implemented outside the serializable classes. /pwm On Tue, 29 Apr 2008, Derek Clarke wrote: > Although I'd put the fancy stuff in my own stream implementations > rather than the object serialise functions. > > That way you don't spread platform specific code around dozens of > classes, at the possible cost of a little robustness. > > On Tue, Apr 29, 2008 at 9:18 AM, Derek Clarke <de...@ci...> wrote: > > Absolutely. > > > > I just communicated the essence of the concept and left making it > > bombproof as an exercise for the reader :-) > > > > > > > > On Mon, Apr 28, 2008 at 5:31 PM, Per Westermark <pw...@ia...> wrote: > > > But remember that you may quite often want to make the serialize a lot > > > more advanced, with versioning information. > > > > > > Serialize is best when the same version of an application on the same > > > architecture will load a saved state. > > > > > > Life may quickly get complicated if moving between different > > > architectures. If you write a 32-bit int and the other program expects > > > an int to be 64 bits large, the serialize will fail. > > > > > > Another thing is when a new version have added more state information in > > > the class. It is quite probable that the end user expects a new version of > > > an application to be able to continue using the existing state > > > information. > > > > > > I often make the serialize methods use text format (together with a bit of > > > glue helper functions) to make the serialize a bit more forgiving. It is > > > also possible to use binary formats. It's just a question about how to > > > handle potential overflows, and to make sure that missing/invalid fields > > > gets reasonable defaults. > > > > > > /pwm > > > > > > > > > > > > On Mon, 28 Apr 2008, Derek Clarke wrote: > > > > > > > A good way is to add serialise operators to the class: > > > > > > > > ostream& hero::operator>>(ostream& stream) > > > > { > > > > mapid >> stream; > > > > posx >> stream; > > > > posy >> stream; > > > > return stream; > > > > } > > > > > > > > istream& hero::operator <<(istream& stream) > > > > { > > > > mapid << stream: > > > > posx << stream; > > > > posy << stream; > > > > return stream; > > > > } > > > > > > > > On Mon, Apr 28, 2008 at 6:40 AM, Samuel Hulley <tal...@gm...> wrote: > > > > > Hi, > > > > > I am attempting to load variables from files but im not sure what > > > > > todo because the variables are different types and sizes. > > > > > Here is the source: > > > > > > > > > > #pragma once > > > > > #include <fstream> > > > > > using namespace std; > > > > > > > > > > class hero > > > > > { > > > > > public: > > > > > void setname(string first, string last) > > > > > { > > > > > fname = first; > > > > > lname = last; > > > > > return; > > > > > } > > > > > > > > > > void setmap(int mp) > > > > > { > > > > > mapid = mp; > > > > > return; > > > > > } > > > > > > > > > > void setpos(int x, int y) > > > > > { > > > > > posx = x; > > > > > posy = y; > > > > > return; > > > > > } > > > > > > > > > > void setmapx(int x) > > > > > { > > > > > posx = x; > > > > > return; > > > > > } > > > > > > > > > > void setmapy(int y) > > > > > { > > > > > posy = y; > > > > > return; > > > > > } > > > > > > > > > > int getmap() > > > > > { > > > > > return mapid; > > > > > } > > > > > > > > > > int getmapx() > > > > > { > > > > > return posx; > > > > > } > > > > > > > > > > int getmapy() > > > > > { > > > > > return posy; > > > > > } > > > > > > > > > > void loadinfo() > > > > > { > > > > > // This is where I will load the config. > > > > > } > > > > > > > > > > private: > > > > > // Map variables. > > > > > int mapid; > > > > > int posx; > > > > > int posy; > > > > > }; > > > > > > > > > > Yours Truly > > > > > Samuel Hulley. > > > > > > > > > > ------------------------------------------------------------------------- > > > > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > > > > Don't miss this year's exciting event. There's still time to save $100. > > > > > Use priority code J8TL2D2. > > > > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > > > > _______________________________________________ > > > > > Dev-cpp-users mailing list > > > > > Dev...@li... > > > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > > > > > ------------------------------------------------------------------------- > > > > This SF.net email is sponsored by the 2008 JavaOne(SM) Conference > > > > Don't miss this year's exciting event. There's still time to save $100. > > > > Use priority code J8TL2D2. > > > > http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone > > > > _______________________________________________ > > > > Dev-cpp-users mailing list > > > > Dev...@li... > > > > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > > > > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > > > > > > > > > > > > > |