- Home /
Problem is not reproducible or outdated
Using StreamReader
Hi there. I have a text file with data that I want to read. I want to be able to search through the file until I find a specific piece of data. My code thus far is (aside from terrible):
public int numPlanets;
private string[,] planNameOwnImprov = new string[6,3];
private string text = " ";
void Start()
{
LoadFile();
}
void LoadFile()
{
StreamReader reader = new StreamReader("SystemTypeData.txt");
while(text != null)
{
text = reader.ReadLine();
if(text == gameObject.name)
{
text = reader.ReadLine();
numPlanets = int.Parse (text);
for(int i = 0; i < numPlanets; i++)
{
text = reader.ReadLine();
planNameOwnImprov[i,0] = text;
planNameOwnImprov[i,1] = "no";
planNameOwnImprov[i,2] = "poor";
}
break;
}
}
}
Can anyone offer any help as to what to do. There seems to be nearly nothing useful on the unity forums or answers that I could find. I would just like some generic code to set up the file for I/O. Using C#. Thanks very much!
well streamreader is something in C#, so search more general for the use of streamreader.
If you can change the data format of the SystemTypeData.txt file, maybe the best thing would be using an xml file. There is plenty of classes to read and write them and you wouldn't have to debug your code for parsing configuration files, as you would use standard functions made and tested by others.
"There seems to be nearly nothing useful on the unity forums or answers that I could find" - that's because you have a non-Unity related text reading problem.
Let me clarify this a bit. I have used streamreader before many times. I don't know how to implement it in unity. Where should I be placing my txt files? How do i access the text file from unity?
You would have to give a path to the streamreader.
For being able to use the same path when you make a .exe to run anywhere I'd put it somewhere in the assets folder.
You might need System.IO to get the path so it will work on any pc.
There is enough info on that on the internet.
Thanks :) I really appreciate the help.
Why did you make a clarification in an answer? Use a comment on the original post if you don't want to edit it and remove this Non-answer from the "answer" section.
If I were a mod I'd close this question as being off topic.
Answer by hameed-ullah-jan · Aug 29, 2018 at 07:00 AM
is there any way to read text file, out side the directory of the project, bcz whenever i try to read a text file outside the project directory it gives null reference (some part of the path is missing).
What is the purpose of accessing a file outside the project directory? Why would you ever do this? Generally programs should be confined to their boxes, that is: be self contained.
there can be many situations in which you need to access files outside the project directory, in my case, i'm working on a big project, i'm using external camera and laser beam, i'm storing the co-ordinates of that laser beam in a text file which is to be stored in some other directory, so that when i build my unity project, i have to get that co-ordinates from my PC, that's why i needed this thing. there can be many other situations in which programmers need to access files outside the project directory.
that seems like an arbitrary opinion. there are tons of use cases that involve accessing files outside of a project build directory whether by relative path or absolute path.
i found the solution, here it is: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO; using UnityEngine.UI;
public class ReadingFile : $$anonymous$$onoBehaviour { StreamReader _textreader; public Text mytext; string text = ""; public string url = "file://c:/Users/Research-PC/Desktop/textEXP.txt"; // Use this for initialization void Start () { //StartCoroutine(LoadFile ());
}
// Update is called once per frame
void Update () {
StartCoroutine(LoadFile ());
}
IEnumerator LoadFile()
{
using (WWW www = new WWW(url))
{
yield return www;
text = www.text;
Debug.Log (text);
mytext.text = text;
}
}
}
Answer by Robotic_Soul · Aug 29, 2018 at 07:53 AM
This is not a Unity question. It's a programming question, and the obvious answer to me is to Read the Docs: https://docs.microsoft.com/en-us/dotnet/api/system.io.stream?view=netframework-4.7.2
If that's too confusing you could try reading this: https://www.tutorialspoint.com/csharp/csharp_file_io.htm
Then if you still need help you should read the other top 5 google links (where I found these). In short, do some basic homework before asking questions.
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
Saving final score and displaying on main menu 1 Answer
null texture passed to GUI.DrawTexture 0 Answers