Help with StreamReader / Reading Text File

Hey guys,

So I’ve found lots of examples about reading / writing to text files. I tried to figure this out myself but I’m still new to C#

making an RPG game and I’m storing the game characters attributes in a text file. I was able to figure out how to store them line by line but I’m having troubles figuring out how to read each line of the text file and then assigning each line to a variable. some of them will need to be strings but most ints.

here is an example of how i stored the variables to a text file:

  File.AppendAllText (usersPath, playerHealth + Environment.NewLine);
  File.AppendAllText (usersPath, playerMana + Environment.NewLine);
  File.AppendAllText (usersPath, playerStrength + Environment.NewLine);
  File.AppendAllText (usersPath, playerDex + Environment.NewLine);

whats the easiest way to read each line and store that line to a var?

I’m assuming i can use file.ReadAllText or I’d have to use System.IO.StreamReader?

Thanks for any help.

The only example I could find in my projects is from a script I found on the net, maybe it can help you understand what’s possible when readin a text file.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class WriteToFile : MonoBehaviour
{
	private Color varToGet;
	private Color varToGet2;
	private TextAsset textFileVar;
	private string textFileToString;





	void Awake() {
		textFileVar = Resources.Load("Scripts/blubb") as TextAsset;


	}

	void Start() {
		Main ();
		textFileToString = textFileVar.text;
		var stringToDic = ParseColorTable(textFileToString);
		varToGet = stringToDic["skinColor"];
		varToGet2 = stringToDic["hairColor"];
	}

	public static void Main() {
		// Create an instance of StreamWriter to write text to a file.
		// The using statement also closes the StreamWriter.
		using (StreamWriter sw = new StreamWriter("Assets/Resources/Testfiles/TestFile.txt")) {
			Debug.Log ("writing to file");
			// Add some text to the file.
			sw.Write("This is the ");
			sw.WriteLine("header for the file.");
			sw.WriteLine("-------------------");
			// Arbitrary objects can also be written to the file.




		}
		System.IO.File.AppendAllText("Assets/Resources/Testfiles/TestFile.txt", System.String.Format("{0} {1} {2} {3} {4}", "string1", "string2", "float1", "float2", "float2"));
	}

	Dictionary<string, Color> ParseColorTable(string aText)	{
		
		Dictionary<string, Color> result = new Dictionary<string,Color>();
		
		string[] lines = aText.Split('

‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');

				// Remove all spaces and split the 4 color values
				string[] values = S[0].Replace(" ","").Split(',');
				
				// Parse the 4 strings into floats and create the color value
				Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
				
				// Read the colorname and remove leading or trailing spaces
				string colorName = S[1].Trim();
				
				result.Add(colorName,col);
			}
		}
		return result;
	}


	Dictionary<string, Color> ParseFillTable(string aText)	{

		Dictionary<string, Color> result = new Dictionary<string,Color>();
		
		string[] lines = aText.Split('

‘);
foreach (string L in lines) {
if (L.StartsWith(“RGBA(”)) {
// Cut “RGBA(” and split at “)”
string S = L.Substring(5).Split(’)');

				// Remove all spaces and split the 4 color values
				string[] values = S[0].Replace(" ","").Split(',');
				
				// Parse the 4 strings into floats and create the color value
				Color col = new Color(float.Parse(values[0]),float.Parse(values[1]),float.Parse(values[2]),float.Parse(values[3]));
				
				// Read the colorname and remove leading or trailing spaces
				string colorName = S[1].Trim();
				
				result.Add(colorName,col);
			}
		}
		return result;
	}
}