Re: saving an array, bgt

You could save the array elements in a file and then parse the array at load time
let's assume we have an object named person
class person
{
//person properties
string name;
int age;
person(string name, int age)
{
this.name = name;
this.age = age;
}
}
now, we set up an array of persons
person@ persons(0);
/* this function will save the array in a file, if the array has any element*/
void savepersons()
{
// let's create an huge string, which contains the data that has to be written
string final;
//let's iterate throughg the array
for(int j=0; j<persons.length; j++)
{
final+=persons[j].name+" "+persons[j].age+"\r\n";
}
//let's write in a file
file f;
f.open("data.dat", "wb");
f.write(final);
f.close();
}
As you saw, I separated the 2 elements by a space, and separated this record with a carriage return.
In this way, when loading I can parse the objects in the array by simply splitting the file data, and and splitting each record. I know it sounds confusing, and my english is not helping at all, but here we go
void loadpersons()
{
file f;
f.open("data.dat", "rb");
//let's now split the data we need
string[] data_array = string_split(f.read(), "\r\n", false);
for(int j=0; j<data_array.length; j++)
{
string[] record = string_split(data_array[j], ":", false);
//now with this record, we can instantiate the class and load it in the array
person p(record[0], record[1]);
persons.insert_last(p);
}
}
Hope it helped.
If you still have some doubts, feel free to ask.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — New releases room : Zarvox via Audiogames-reflector
    • ... AudioGames . net Forum — New releases room : SkyGuardian via Audiogames-reflector
    • ... AudioGames . net Forum — New releases room : janagirl via Audiogames-reflector
    • ... AudioGames . net Forum — New releases room : Zarvox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : redfox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : pauliyobo via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Zarvox via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : ogomez92 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : ogomez92 via Audiogames-reflector

Reply via email to