0

I am using the following code to save data to a file:

void save()
{
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open(Application.persistentDataPath + "/MyData.dat",FileMode.Append);
    MyData data = new MyData ();
    data.s = input.text;
    input.text="";
    bf.Serialize (file, data);
    file.Close ();
}

And the following code to load data from the saved file:

public void load()
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath+"/MyData.dat",FileMode.Open);
    MyData data = (MyData)bf.Deserialize(file);
    file.Close();
    name = data.s;  //returns only one string
}

[Serializable]
class MyData
{
    public string s;
}

I am able to append multiple strings to a file, but when I am accessing them using

name = data.s;

it only returns a single string. Can anyone help me to sort out how ho print all the strings saved in the file.

anothershrubery
  • 20,461
  • 14
  • 53
  • 98
toadalskiii
  • 136
  • 2
  • 14

1 Answers1

0

That is because in save() you appending not a new string but a new MyData object, contained single string. And in load() you are loading only first serialized MyData object.

  • Now, I am using sqlite instead of seriazable class. It would be great if you could help me with this http://stackoverflow.com/questions/29944561/copy-sqlite-database-from-assets-folder-to-android-persistentpath-in-unity3d – toadalskiii Apr 29 '15 at 13:12