Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 $$anonymous$$ · Nov 23, 2014 at 12:13 AM · c#arrayfor-looptext file

How to save a 2d-array in C#

I have been looking to save a 2d-array from my terrain component to a file of some sorts. I have tried to save it to a .txt but for some reason, it did not finish? I even ran the code through a coroutine and it have me incorrect results, if im not mistaken...

My 2d-array is [296, 296] so that would be 296^2, which is 87616, but the text file only produced around 65574 lines (I had a counter value which was printed on the text file for debug reasons.). I even put the text file into mono-develop just to make sure it wasn't a glitch with notepad but, it displayed the same amount of lines as my counter in the text file did. I got added some further debug values in the text and realised that my for loop, which I was using for the repetition, only seemed to go up to [255, 38] which I found odd.

Here is the code. (I edited it a bit so that the for loop values are displayed on a separate line from the array value, but all you have to do is divide the last line's number by 2...) IEnumerator Write() { // Output Stream Writer variable StreamWriter yourOSW;

         // Open the file
         yourOSW = new StreamWriter("C:/Users/" + Environment.UserName + "/Desktop/IslandData.txt"); 
         
         
         // To write array to file
         for(int i = 0; i < xRes; i++)
         {
             for(int j = 0; j < zRes; j++)
             {
                 yourOSW.WriteLine(i.ToString() + " " + j.ToString() + " " + Environment.NewLine + heights[i,j]);
             }
         }
         yield return null;
     }

i & j both have the values of 296.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by zharik86 · Nov 23, 2014 at 08:20 AM

I'm not sure, but a good practice is to use these objects in a using statement so that the unmanaged resources are correctly disposed. The using statement automatically calls Dispose on the object when the code that is using it has completed. Try this:

  using (yourOSW = new StreamWriter("C:/Users/" + Environment.UserName + "/Desktop/IslandData.txt")) {
   // To write array to file
   for(int i = 0; i < xRes; i++) {
    for(int j = 0; j < zRes; j++) {
     yourOSW.WriteLine(i.ToString() + " " + j.ToString() + " " + Environment.NewLine + heights[i,j]);
    }
   }
  }

But, if it's not help, use method for string:

  // To write array to file
  string str = "";
  for(int i = 0; i < xRes; i++) {
   for(int j = 0; j < zRes; j++) {
    str = str + (i.ToString() + " " + j.ToString() + " " + Environment.NewLine + heights[i,j]) +"\n";
   }
  }
  //Write all text into file, but remember: path to file must be
  System.IO.WriteAllText(str, "C:/Users/" + Environment.UserName + "/Desktop/IslandData.txt");

I hope that it will help you.

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 $$anonymous$$ · Nov 23, 2014 at 12:09 PM 0
Share

Ok, ill try it. (P.S. I did not use a 'using' statement before anyway, but I will try this. Thank You for replying though! :)

avatar image
1

Answer by Jignesh G. · Nov 23, 2014 at 06:52 PM

The best way is to mark field as [Serializable] and you can serialize and deserialize field according to require. this will save your 2D Array.

Another way is to convert it to byte array and you can store byte array in preferences.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

How to deactivate all GameObject in a array, except last one 4 Answers

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

C# For loop in button to set gameObjects in array to active 1 Answer

Array Overflow Problem 1 Answer


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