14 Replies - 1374 Views - Last Post: 13 November 2009 - 08:40 AM Rate Topic: -----

#1 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Saving an Array to a text file

Post icon  Posted 13 November 2009 - 06:00 AM

Hi, does anyone know how I can save an Array to a text file.

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Saving an Array to a text file

#2 batesy3k  Icon User is offline

  • D.I.C Regular

Reputation: 41
  • View blog
  • Posts: 299
  • Joined: 10-September 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:06 AM

what sort of array have you got? please be more specific in your questions
Was This Post Helpful? 0
  • +
  • -

#3 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:11 AM

A 2d array.
Was This Post Helpful? 0
  • +
  • -

#4 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:11 AM

using (StreamWriter sw = File.CreateText(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\TextFile.txt"))
{
	foreach (string line in NameOfArray)
	{
		sw.WriteLine(line);
		sw.WriteLine();
	} // foreach
} // using



this will create a text file called TextFile.txt in the MyDocuments folder (Documents folder for Vista and later). You just have to replace "NameOfArray" with the name of your array. Please keep in mind you didn't mention what kind of array it was so I just went with the standard string array.
Was This Post Helpful? 1
  • +
  • -

#5 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:17 AM

Thanks for that, how about if I wanted to load it again?
Was This Post Helpful? 0
  • +
  • -

#6 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:27 AM

According to MSDN at the following site
http://msdn.microsof...reamwriter.aspx
StreamReader should do the trick for you.

Here is their sample code for this one:
// Read and show each line from the file.
string line = "";
using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
{
	while ((line = sr.ReadLine()) != null)
	{
		Console.WriteLine(line);
	}
}



hope this was helpful. the other one I am using on a current project so I know it works. This I just found on MSDN.
Was This Post Helpful? 0
  • +
  • -

#7 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:43 AM

Thanks, i've had a play with it dataand I can't get it to load the data from the text file back into an array as you can't feed the data from the strign into it. Does anyone know how to fix this?
Was This Post Helpful? 0
  • +
  • -

#8 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 06:54 AM

if you use an array list instead of just a normal array you could say

ArrayList blah = new ArrayList();

while ((line = sr.ReadLine()) != null)
{
	blah.Add(line);
}

string[] basic = new string[blah.Count];

basic = blah.ToArray();



in this case the Array List blah, at the end of the read would contain all the info from the text file. then when you use the "blah.ToArray()" you can transfer it into the actual array that you are trying to use. I haven't actually tried this out yet, but it should work out for ya. Let me know!

This post has been edited by Adkins: 13 November 2009 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#9 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 07:15 AM

Ok, tried it and got the following error.

Cannot implicitly convert type 'object[]' to 'int[*,*]'
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Saving an Array to a text file

Posted 13 November 2009 - 07:17 AM

Show YOUR code, Jackal16. You know, the stuff that's failing???
Was This Post Helpful? 0
  • +
  • -

#11 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 07:19 AM

Where did the error come from? Which line, ect.?
Was This Post Helpful? 0
  • +
  • -

#12 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 07:27 AM

 
public static void LoadGame()
		{
			string line = "";
			using (StreamReader sr = new StreamReader("\\TextFile.txt"))
			{
				ArrayList blah = new ArrayList();

				while ((line = sr.ReadLine()) != null)
				{
					blah.Add(line);
				}
			}
	 -->	board = blah.ToArray();

		}


The line with the arrow pointing at it.

This post has been edited by Jackal16: 13 November 2009 - 07:28 AM

Was This Post Helpful? 0
  • +
  • -

#13 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 08:04 AM

Is board an array? is it a string array? Can you post the definition of it?
Was This Post Helpful? 0
  • +
  • -

#14 Jackal16  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-November 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 08:10 AM

Board is an int array which is 2d.

This post has been edited by Jackal16: 13 November 2009 - 08:10 AM

Was This Post Helpful? 0
  • +
  • -

#15 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Saving an Array to a text file

Posted 13 November 2009 - 08:40 AM

get the feeling you are not even trying. Okay to turn a string into an int you use

int ctr = 0;
string test = "32";

ctr = Int32.Parse(test);


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1