Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 29, 2018 at 08:44 AM by Shemamforash for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Shemamforash · Nov 06, 2013 at 11:28 AM · c#file-io

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!

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thef1chesser · Nov 06, 2013 at 11:44 AM 1
Share

well streamreader is something in C#, so search more general for the use of streamreader.

$$anonymous$$SDN site on Streamreader

avatar image Azrapse · Nov 06, 2013 at 12:53 PM 1
Share

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.

avatar image Graham-Dunnett ♦♦ · Nov 06, 2013 at 04:09 PM 0
Share

"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.

avatar image Shemamforash · Nov 06, 2013 at 07:19 PM 0
Share

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?

avatar image thef1chesser Shemamforash · Nov 07, 2013 at 09:21 AM 0
Share

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.

avatar image Shemamforash Shemamforash · Nov 07, 2013 at 09:40 AM 0
Share

Thanks :) I really appreciate the help.

avatar image Robotic_Soul Shemamforash · Aug 29, 2018 at 07:46 AM -1
Share

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.

avatar image Robotic_Soul · Aug 29, 2018 at 07:51 AM -1
Share

If I were a mod I'd close this question as being off topic.

2 Replies

  • Sort: 
avatar image
1

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).

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Robotic_Soul · Aug 29, 2018 at 07:40 AM 0
Share

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.

avatar image hameed-ullah-jan Robotic_Soul · Aug 31, 2018 at 08:53 AM 1
Share

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.

avatar image brunzero Robotic_Soul · Aug 06, 2019 at 07:04 PM 0
Share

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.

avatar image hameed-ullah-jan · Aug 29, 2018 at 08:28 AM 0
Share

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;

     }
 }

}

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges