Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

C# How save data from array in file on disk

Discussion in 'Scripting' started by aradriel, Jul 17, 2012.

  1. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    I have array of integers ar[10] please example of code, how to save 10 its elements to file on disk.
    And then - open it and get back into array.
     
    skoteskote likes this.
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    oasf likes this.
  3. znoey

    znoey

    Joined:
    Oct 27, 2011
    Posts:
    174
    +1 marrrk
     
  4. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    English is not my native language - searching among tons of english texts is not so easy. Google have all answers, but i ask my questions exactly here. Thank you.
     
  5. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    So on.

    I trying to use WriteAllBytes

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class IOscript : MonoBehaviour {
    6.    
    7. public byte [] World;  
    8.    
    9.    
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.         World = new byte[10];
    14.         World[1]=10;
    15.         World[6]=7;
    16.        
    17.            
    18.         BinaryWriter bw;
    19.         bw=File.WriteAllBytes("data.dat",World[10]);
    20.        
    21.            
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28. }
    Result error:

    and

    Who knows, how to save data from array to file, at the end? Please help. (In example)

    (If u dont know, please do not flood here)
     
    Last edited: Jul 17, 2012
  6. Diviner

    Diviner

    Joined:
    May 8, 2010
    Posts:
    677
    Change

    Code (csharp):
    1.  
    2. bw=File.WriteAllBytes("data.dat",World[10]);
    3.  
    to

    Code (csharp):
    1.  
    2. bw=File.WriteAllBytes("data.dat",World);
    3.  
     
  7. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    Yes, i was thoght about it, but this give error:

    It means World without [] is null or something.
     
  8. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Nope. File.WriteAllBytes doesn't return a value but you're trying to assign it's result to one.
     
  10. aradriel

    aradriel

    Joined:
    Nov 28, 2011
    Posts:
    47
    I found!

    Need to use

    Code (csharp):
    1.         FileStream fs;
    2.         fs=File.Create("data.dat");
    3.         fs.Write(World,1,5);
    4.         fs.Close ();
    =) All works! Thnx to all!
     
    BelinExperience likes this.
  11. jgibbens

    jgibbens

    Joined:
    Feb 26, 2013
    Posts:
    2
  12. bhison

    bhison

    Joined:
    Feb 9, 2015
    Posts:
    9
    Considering this came at the top of my search results and I wouldn't have figured out what to do with just the SO link, I am very appreciative of this thread.
     
    GameDeveloper1111 and skoteskote like this.