Re: [Dev-C++] Loading variables from files [C++]
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
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
>
|