• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by missypooh · Mar 31, 2012 at 02:53 AM · file-iofilestream

Why data will be overwrite in the file

This is working fine but i realise that the file will be overwrite every time a new game is play which is not i want. I want to maintain the data. Instead of overwrite the data, i want to add on to the data. Any thing 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!!! :((

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Text;
 using System.IO;
 
 public class Monitor : MonoBehaviour {
     
     String path;
     String fileName;
     String playerName;
     int id =1;
     // Use this for initialization
     void Start () 
     {
         fileName = "/Monitoring.txt";
         playerName = playerControl.playerName;
         print(gameControl.previousWord1);
     }
     
     // Update is called once per frame
     void Update () 
     {
         //string messageToAccess = gameControl.previousWord1;
         path = Application.dataPath + fileName;
         
         using(FileStream fs = File.Create(path))
         {
             AddText(fs, "PlayerName: " + playerName);
             AddText(fs, "\r\n================================");
             AddText(fs, "\r\n" + id);
             for(int j = 0; j < gameControl.radical00Store.length; j++)
             {
                 AddText(fs, "\t" + gameControl.radical00Store[j]);
                 AddText(fs, "\t" + gameControl.radical0Store[j]);
                 AddText(fs, "\t" + gameControl.previousWord1[j]);
             }
             AddText(fs, "\t" + gameControl.wrongMatch + "\n");
         }
         id++;
         
          //Open the stream and read it back.
         using (FileStream fs = File.OpenRead(path))
         {
             byte[] b = new byte[1024];
             UTF8Encoding temp = new UTF8Encoding(true);
             while (fs.Read(b,0,b.Length) > 0)
             {
                 Console.WriteLine(temp.GetString(b));
             }
         }
     }
     
     
     private static void AddText(FileStream fs, string value)
     {
         byte[] info = new UTF8Encoding(true).GetBytes(value);
         fs.Write(info, 0, info.Length);
     }
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Mar 31, 2012 at 03:53 AM

Look at File.Open (instead of create.) It has an Append option to add to the end. But, in practice, you rarely want to write to the end - you want to modify what's there. To do that, read the file into an array make changes (like add 1 to games played,) then erase the old file and write it all back.

For names, you can make the name part of the fileName: MonitoringStan.txt, and have 1 file for each player. Most people would have one file look like: name, stats for name; 2nd name, stats for 2nd name; ... . Then have the reader and writer know to keep reading/writing names until EOF.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image missypooh · Mar 31, 2012 at 04:11 AM 0
Share

Hi Owen, thank for the answer. Are you able to provide some code snippet??

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to write a file to local using IEnumerator? 1 Answer

Text file in resource folder only gets updated when i quit. 1 Answer

Write to online file with webplayer 1 Answer

Can not save data to file and then load it in Unity3d 2 Answers

3 Arrays of data write to file and then display to player 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges