Search in a text file

Hello,

I have a script that creates, and writes into a text file. My question is, how can I search in the file? For example, I want to edit the line under the line that contains Level2. How can I do it?

Here is my code:

import System.IO;

var filePath = "data.txt";

function Start()
{
	if (!File.Exists(filePath))
	{
		CreateFile();
	}
}

function CreateFile()
{
	var sw : StreamWriter = new StreamWriter(filePath);
	sw.WriteLine("profile");
	sw.WriteLine("Level1");
	sw.WriteLine("unlocked");
	sw.WriteLine("Level2");
	sw.WriteLine("Locked");
	sw.Flush();
	sw.Close();
	print ("Done");
}

1 Answer

1

Read it back and look at the lines. Instead of StreamWriter, you use StreamReader, etc., etc.

To change it, change what you have in memory and write it back out again.

So if I would like to enable level 2, than I should search for level 2, then rewrite the 2 lines? The code above is just an example, my full file will be 150+ lines long.

No, you'd just rewrite the entire file. Looks to me like you should just be using PlayerPrefs anyway.

How exactly would this look like? Find(ParenObject ChildObjectName ) ?