- Home /
 
.txt File Bug
Hi. I'm working on a game where it reads info from a file and overwrites the file whenever i need it to. For some reason (which i dont know what the reason is), blank lines keep "spawning" in the .txt file. Heres some of the code. I hope this helps:
 function Start () { 
     var sr = File.OpenText("Assets/XXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
 } 
 
 function OnCollisionEnter (col : Collision) {
     var sr = File.OpenText("Assets/XXXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
     WriteHealth();
 }
 
 function WriteHealth () {   
     
     var ar = fileLines.ToArray();
     
     if (transform.parent.name == "XXXXX") { 
     ar[0] = health.ToString();
     }
     if (transform.parent.name == "XXXX") {
     ar[1] = health.ToString();
     } 
     File.WriteAllLines("Assets/XXXXXX/Files/charactersHealth.txt", ar);
 }
 // Some personal info is blanked out with X's
 
               Also, other scripts with kind-of the same type of code is accessing the same file as the code above is.
Thanks for your help, -GameDude
well, what happens is that in the file, it starts out with two lines which has two values. Then when i test the game, every time the character collides with something, 7 blank lines appears after the two values. And it keeps happening and soon i have at least 300 unnecessary blank lines.
By the way, is there any way where i can delete the unnecessary blank lines from the .txt file through the script?
I'm rather bussy now and its kinda late, so I'll get back to you tomorrow, but I belive if you do a null check before writing in the txt file your problem will be solved
Ohh. That makes alot of sense. But how can you do that?
Answer by zardini123 · Jan 06, 2013 at 12:48 AM
Ok i figured it out. I used File.WriteLine instead. Hears some of the code:
 function WriteHealth () {   
     var ar = fileLines.ToArray();
     var blankArray : String[] = [""];
     
     if (transform.parent.name == "XXXXX") { 
         ar[0] = currentHealth.ToString();
     }
     if (transform.parent.name == "XXXXX") {
         ar[1] = currentHealth.ToString();
     } 
        var sw : StreamWriter = new StreamWriter("Assets/XXXXXXX/Files/charactersHealth.txt");
 
     sw.WriteLine(ar[0]);
     sw.WriteLine(ar[1]);
 
     sw.Flush();
     sw.Close();
 }
 
               Now there is only one blank line no matter what. One blank line isn't that bad.
Answer by Bunny83 · Jan 06, 2013 at 12:33 AM
There is no "bug" in a txt file and also not in the File class. Everything works as it should. The problem here is that you use "WriteAllLines" which usually uses windows line endings (CR + LF). Since you split your text manually on LF (`\n`) the CR (`\r`) will remain in the text and will be written to the file as additional lines.
The File class has a lot quite useful static functions like File.ReadAllLines() and File.WriteAllLines. I'm not sure why you use a File object to read the file and WriteAllLines to write the file.
So just use File.ReadAllLines() to read the file and everything should be fine.
Answer by Mr.Z · Jan 05, 2013 at 02:19 PM
Alright, try this:
 function Start () { 
     var sr = File.OpenText("Assets/XXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
 } 
 
 function OnCollisionEnter (col : Collision) {
     var sr = File.OpenText("Assets/XXXXXX/Files/charactersHealth.txt");
     fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
     sr.Close(); 
     WriteHealth();
 }
 
 function WriteHealth () {   
 
     var ar = fileLines.ToArray();
     if (transform.parent.name == "XXXXX") { 
     ar[0] = health.ToString();
     }
     if (transform.parent.name == "XXXX") {
     ar[1] = health.ToString();
     }
 if(ar[o] && ar[1]){
             File.WriteAllLines("Assets/XXXXXX/Files/charactersHealth.txt", ar);
 }
 
              Uh at this part: catch (Exception e){ throw new Exception(e.ToString()); } What is e?
I get two errors when i inputted the code which are: -Unexpected token: e -Unexpected token: throw
Hahaha wow. At least most of it works. Please just translate the stuff into JS. Thanks
Your answer
 
             Follow this Question
Related Questions
Reading a file's memory size 1 Answer
"Opening file fail"?? 3 Answers
C# How do I read an external text file asynchronously in Unity? 2 Answers
Green lines are gone? 1 Answer