- Home /
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.
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.
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! :)
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.
Your answer
Follow this Question
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