Search Unity

Why data is being overwrite after we start a new games

Discussion in 'Scripting' started by missy_pooh, Mar 31, 2012.

  1. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    The code below working fine but i realise that the file will be overwrite every time a new game is play which is not what i want. I want to maintain the data. Instead of overwrite the data, i want to add on to the data. And what bother me is regarding, how we know this file is belong to which players. Is it any possible way i can solve this issues? Like this data in this file is the record play by may.. Please help!!! :((

    Please look at the link of the picture below. This is the format on how i want to save it into my file. Like store the record of each player. I am guessing i need to read the content in the file first (like compare whether this file is belong to this player, then if it is, then i start to write the data to file?)

    http://imageshack.us/photo/my-images/213/textfile1.png/

    Code (csharp):
    1.  
    2.     using UnityEngine;
    3.     using System.Collections;
    4.     using System;
    5.     using System.Text;
    6.     using System.IO;
    7.    
    8.     public class Monitor : MonoBehaviour {
    9.        
    10.         String path;
    11.         String fileName;
    12.         String playerName;
    13.         int id =1;
    14.         // Use this for initialization
    15.         void Start ()
    16.         {
    17.             fileName = "/Monitoring.txt";
    18.             playerName = playerControl.playerName;
    19.             print(gameControl.previousWord1);
    20.         }
    21.        
    22.         // Update is called once per frame
    23.         void Update ()
    24.         {
    25.             //string messageToAccess = gameControl.previousWord1;
    26.             path = Application.dataPath + fileName;
    27.            
    28.             using(FileStream fs = File.Create(path))
    29.             {
    30.                 AddText(fs, "PlayerName: " + playerName);
    31.                 AddText(fs, "\r\n================================");
    32.                 AddText(fs, "\r\n" + id);
    33.                 for(int j = 0; j < gameControl.radical00Store.length; j++)
    34.                 {
    35.                     AddText(fs, "\t" + gameControl.radical00Store[j]);
    36.                     AddText(fs, "\t" + gameControl.radical0Store[j]);
    37.                     AddText(fs, "\t" + gameControl.previousWord1[j]);
    38.                 }
    39.                 AddText(fs, "\t" + gameControl.wrongMatch + "\n");
    40.             }
    41.             id++;
    42.            
    43.              //Open the stream and read it back.
    44.             using (FileStream fs = File.OpenRead(path))
    45.             {
    46.                 byte[] b = new byte[1024];
    47.                 UTF8Encoding temp = new UTF8Encoding(true);
    48.                 while (fs.Read(b,0,b.Length) > 0)
    49.                 {
    50.                     Console.WriteLine(temp.GetString(b));
    51.                 }
    52.             }
    53.         }
    54.        
    55.        
    56.         private static void AddText(FileStream fs, string value)
    57.         {
    58.             byte[] info = new UTF8Encoding(true).GetBytes(value);
    59.             fs.Write(info, 0, info.Length);
    60.         }
    61.        
    62.     }
    63.  
     
  2. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    Yup. Sounds reasonable to me.

    Edit: You could perhaps name the file according the the player. That way you won't have to actually open and read the contents of the file first, but mere have to check if it exists.
     
  3. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    You really really really shouldn't use FileStream inside of Update() lol.

    If you really have to write stuff on each frame, use MemoryStream. Everything happens inside the memory and when you want to save it just dump it to a file: memoryStream.Copyto(fileStream).

    Also, with MonoBehaviour you put your component dependant initialization into "Awake()" (like creating/instantiating subobjects/children, etc.) and use "Start()" to get References to other objects (i.e. GameObject.Find, Tramsform.Find, someotherObject.GetComponent etc.).

    You may want to use
    Code (csharp):
    1.  
    2. using(FileStream fs = new FileStream(path, FileMode.Append, FileAccess.ReadWrite)) {
    3. }
    4.  
    However, creating and closing streams on each update is very bad. So use the suggestions above and use MemoryStream instead and dumb it on the disk only when the user hits a button or quits a level etc.
     
  4. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Yes. I am thinking of that way too. But how could i actually do that??
     
  5. missy_pooh

    missy_pooh

    Joined:
    Jun 1, 2011
    Posts:
    150
    Hi, thank you. Anyway so you are saying that i should remove the create file outside the update function and put it in the start function instead?
     
  6. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    Yea, creating/opening a file >=60 times per second is a pretty bad idea.

    It's best not to use FileStream at all until you have to save it.

    Code (csharp):
    1.  
    2. public class Monitor : MonoBehaviour {
    3.  
    4.     private MemoryStream ms;
    5.  
    6.     void Start() {
    7.         ms = new MemoryStream(102400); // reserve some memory for it, like 100kb or whatever seems to fit your needs
    8.     }
    9.  
    10.     void Update() {
    11.         AddText(....);
    12.  
    13.  
    14.         if(Input.GetKeyUp(KeyCode.F5)) {
    15.             SaveToFile("logs/monitor-playername.log");
    16.         }
    17.     }
    18.  
    19.     public void SaveToFile(string path) {
    20.         using(FileStream fs = new FileStream(path, FileMode.Append, FileAccess.ReadWrite)) {
    21.             ms.CopyTo(fs);
    22.             ms.Close(); // Close the memory stream if we don't need it anymore.
    23.         }    
    24.     }
    25. }
    26.