- Home /
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");
}
Answer by Waz · Aug 11, 2011 at 11:58 AM
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.
Your answer

Follow this Question
Related Questions
Write a Unicode escape character code to a text file 1 Answer
Finding objects with TextMeshPro component. 1 Answer
Line Break on text mesh not working? 1 Answer
Can't change the 'text' of a 3DText object through a script, no matter what I do. 1 Answer
Javascript/Unityscript or c# String obfuscator ... 2 Answers