- Home /
.txt file issue, first line is skipped
Hello! Im trying to create .txt file and read it`s information. Everything works great exept one thing - first line is skipped. How can i fix this issue?
using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
public class menuManager : MonoBehaviour {
private string path;
string[] lines;
// Use this for initialization
void Start ()
{
path = Application.dataPath+ "/MyTxT.txt";
using (FileStream fs = File.Create(path))
{
for (int i = 0; i < 3; i++)
{
AddText(fs, "\r\nfalse");
}
}
lines = System.IO.File.ReadAllLines(path);
foreach (string line in lines)
{
// Use a tab to indent each line of the file.
Debug.Log(line);
}
windowRect = new Rect (Screen.width/2-100, Screen.height / 2 - 200 , 200, 400);
}
private static void AddText(FileStream fs, string value)
{
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}
Answer by $$anonymous$$ · Nov 13, 2014 at 08:10 PM
AddText(fs, "false\r\n"); Thanks Anxo.
I think, it's bad, because last line in file will be
"\n"
Empty record.
Answer by zharik86 · Nov 13, 2014 at 07:40 PM
See you code:
AddText(fs, "\r\nfalse");
You first add "\n" - add new line; and them "false". Your file is write and right. Simple change:
string tpStr = "";
for (int i = 0; i < 3; i++) {
if (i > 0) {
tpStr = "\r\n";
} else {
tpStr = "";
}
AddText(fs, tpStr + "false");
}
I hope that it will help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171438im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
start/stop timer when a player enters/exits a room,start/stop timer when player enters/exits a room? 1 Answer
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer