- Home /
Converting StreamWriter to StreamReader
public static int [,,,,,] gridLocation = new int [10,10,10,10,27,7];
I have saved an multi array show on top using the following code.
var path = Application.dataPath + "Save";
if (!Directory.Exists (path)) Directory.CreateDirectory(path);
using(StreamWriter sw = new StreamWriter(path + "/Beyond.txt")) foreach(var item in gridLocation) sw.WriteLine(item);
How do I reverse the process?
I tried this, is there an easier way? This one did not work, cannot convert string to integer. I don't even know if this code will work at all, was unable to test it. Is this the correct way? It would not let me convert to array nor integer.
var path = Application.dataPath + "Save";
if (!Directory.Exists (path)) Directory.CreateDirectory(path);
using(StreamReader sr = new StreamReader(path + "/BeyondTemp.txt"))
for (g = 0; g <=9; g++){ // Galaxy (g)
for (s = 0; s <=9; s++){ // Star (s)
for (p = 0; p <=9; p++){ // Planet (p)
for (m = 0; m <=9; m++){ // Satellite (m)
for (b = 0; b <=26; b++){ // GridBox (b)
for (a = 0; a <=6; a++){ // Area (a)
gridLocation[g,s,p,m,b,a] = sr.ReadLine(); // cannot convert string to int
}}}}}}
Shouldn't you be using a class to hold all those different values, rather than a multi-dimensional array?
umm what do you mean? I have 9 galaxies, each galaxy holds 9 stars, each star holds 9 planets and each planet holds 9 satellites. Each Planet and Satellites can hold a maximum of 27 building grids and each building grid has a total of 6 expansions. I use an int to represent a building number like 1 for command center, 2 for far$$anonymous$$g.
$$anonymous$$yScript.setupGridList(1,1,4,3,1,1,1,5) means take me to Galaxy #1, star #4, planet #3, Satellite #1, Grid #1, and at area #1 has building type #5. And the first number represents 0 = Format, 1 = write, 2 = Read. I found this to be very fast, I didn't notice any slowdown even when saving to a txt format of 3,8 $$anonymous$$B in size. Would there be a better way to do this, I am all open, this is the only one I could think of that worked.
What I am suggesting is that you create $$anonymous$$odel objects for each of these data elements; for example:
public class Galaxy { public List < Star > stars; ... }so the manipulating these objects becomes easier. You could then use C# Serialization techniques to get the objects to/from file: http://msdn.microsoft.com/en-us/library/ms233843.aspx
Answer by kamgru123 · May 22, 2014 at 06:14 AM
You can convert a string to int by using int.Parse()
string s = "123";
int i = int.Parse(s);
O$$anonymous$$ fixed that problem, after retrieving the data at the specific location I got building #5. Same results before and after saving so it works.
Your answer
Follow this Question
Related Questions
Streamwriter Access violation 0 Answers
Problems when saving and loading with FileStream 1 Answer
Problem parsing a file by group of lines 0 Answers
FtpWebRequest and explicit ftp 0 Answers