- Home /
What is best way to save several arrays into a file?
Hi all, I have been fighting this issue for a couple of days and now I have come to think that maybe Im doing something wrong here because errors seem to always remain no matter what I try, I searched here and in other sites and couldnt find the right answer for me. Im trying to save several hi-score tables into a file and later retrieve them of course.
For file storage I created one class that holds only and array or ints and an array of strings, also created a class to store all the hi-score tables called ClaseGuardado:
[System.Serializable]
class Claseguardado
{
public puntoNombre []arrptonmbr;
}
[System.Serializable]
class puntoNombre
{
public int[] pts;
public string[] nombres;
}
So I wrote save and load functions like any other save and load function explained in many sites and threads you know using Binary formatter and serialize or deserialize on the object that holds the data (an instance of ClaseGuardado that has been filled with the data or that we get the data from), then my doubt is, this method is compatible with embedded arrays like these? because I always get errors like the file is empty or the object is not assigned or similar, but I have used this method to save and load simple data before and always worked, and I cant see the reason why its not working aside from the fact that maybe serialize/deserialize cant work with this type of structures. I think it might be as said Im using this when it works only on simple type of data, not something like arrays embedded maybe, anyway here is the code Im using for saving (arrminigames holds the hi scores tables in the game so I have them all in that array), "destino" is the filename destination, I initialize it at the beginning to change only in one place. Hi-scores tables hold a total of 10 names and scores just in case there is any doubt about this:
public void guardar()
{
int i,j;
BinaryFormatter bf;
FileStream file;
Claseguardado cguard;
if (File.Exists(destino))
{
file = File.OpenWrite(destino);
}
else
{
file = File.Create(destino);
}
bf = new BinaryFormatter();
cguard = new Claseguardado();
cguard.arrptonmbr = new puntoNombre[cantiJuegos];
for (i = 0; i < cantiJuegos; i++)
{
cguard.arrptonmbr[i].pts = new int[10];
cguard.arrptonmbr[i].nombres = new string[10];
for (j=0;j<10;j++)
{
cguard.arrptonmbr[i].pts[j] = arrminigames[i].arrpuntos[j];
cguard.arrptonmbr[i].nombres[j] = arrminigames[i].arrnombresHiScores[j];
}
}
bf.Serialize(file, cguard);
file.Close();
}
And this is the one to load:
public void carga()
{
int i, j;
BinaryFormatter bf;
FileStream file;
Claseguardado cguard;
if (File.Exists(destino))
{
bf = new BinaryFormatter();
file = File.OpenRead(destino);
cguard = (Claseguardado)bf.Deserialize(file);
file.Close();
for (i = 0; i < cantiJuegos; i++)
{
for (j = 0; j < 10; j++)
{
arrminigames[i].arrpuntos[j] = cguard.arrptonmbr[i].pts[j];
arrminigames[i].arrnombresHiScores[j]= cguard.arrptonmbr[i].nombres[j];
}
}
}
}
Well as said I appreciate any clue about this issue or if you can direct me the right direction like there is other method to save this type of data or something, I would prefer to use same method of course but only if it can work with this structure i have here.