Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
3 captures
13 Jun 22 - 14 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 Nov 09, 2016 at 03:44 AM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
5
Question by ragnaros100 · Jul 05, 2012 at 01:46 PM · c#saveloadcreatecontinuation

Loading data from a txt file - C#

...Continuation of this

Now I've created a text file with code... Now I just need to read the text i've written down in the .txt -how?

-thanks :)

EDIT: Fixed the link

link to my next question

Comment
Comments Locked · Show 1
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 firemyst · Nov 09, 2016 at 03:28 AM 0
Share

I was looking to do similar things when I stumbled across these answers. Thank you. :-)

These are great and have helped as much as this article providing some good sample code to get started as well in case anyone else is looking too. What I like most about it is it's example incorporates parallel-processing (which wasn't as readily available in 2012 I think).

3 Replies

  • Sort: 
avatar image
16
Best Answer

Answer by Drakestar · Jul 05, 2012 at 02:16 PM

Your link doesn't seem to work.

Generally, you use a streamreader to load in text. Here's some code (untested) that should get you on the right track:

 using System.Text;
 using System.IO;  
 
 private bool Load(string fileName)
 {
     // Handle any problems that might arise when reading the text
     try
     {
         string line;
         // Create a new StreamReader, tell it which file to read and what encoding the file
         // was saved as
         StreamReader theReader = new StreamReader(fileName, Encoding.Default);

         // Immediately clean up the reader after this block of code is done.
         // You generally use the "using" statement for potentially memory-intensive objects
         // instead of relying on garbage collection.
         // (Do not confuse this with the using directive for namespace at the 
         // beginning of a class!)
         using (theReader)
         {
             // While there's lines left in the text file, do this:
             do
             {
                 line = theReader.ReadLine();
                     
                 if (line != null)
                 {
                     // Do whatever you need to do with the text line, it's a string now
                     // In this example, I split it into arguments based on comma
                     // deliniators, then send that array to DoStuff()
                     string[] entries = line.Split(',');
                     if (entries.Length > 0)
                         DoStuff(entries);
                 }
             }
             while (line != null);

             // Done reading, close the reader and return true to broadcast success    
             theReader.Close();
             return true;
             }
         }

         // If anything broke in the try block, we throw an exception with information
         // on what didn't work
         catch (Exception e)
         {
             Console.WriteLine("{0}\n", e.Message);
             return false;
         }
     }
 }
Comment
Comments Locked · Show 11 · 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 ragnaros100 · Jul 05, 2012 at 02:33 PM 1
Share

Do I need to import a package to do this?... And Also. I dont understand how this code works... $$anonymous$$aybe some comments would be nice? :) Also. I fixed the link

avatar image Drakestar · Jul 05, 2012 at 03:03 PM 2
Share

Added some comments and namespaces. I think these are all the libraries used in the code.

avatar image ragnaros100 · Jul 05, 2012 at 03:11 PM 1
Share

So. Where do I specify the line and the text document? (Lets say I have my document in the "C:/$$anonymous$$yGame/data/file.txt" directory, and the code I need is on the 5th line in the document)

avatar image Drakestar · Jul 05, 2012 at 06:08 PM 2
Share

The file name is in the first argument of the StreamReader constructor. It's a string with the path and filename. The reader reads in line after line from the document - to act on the 5th line you could do many things: create an array of strings and add each line in the do/while loop, and then access array[4], or simply put a counter in the loop that detects when you're on line 5.

avatar image Drakestar · Jul 05, 2012 at 06:17 PM 3
Share

By the way, since you're now working with reading/writing text files and encodings, it's probably time to read this article: "The Absolute $$anonymous$$inimum Every Software $$anonymous$$bsolutely, Positively $$anonymous$$ust $$anonymous$$now About Unicode and Character Sets (No Excuses!)" http://www.joelonsoftware.com/articles/Unicode.html

Show more comments
avatar image
10

Answer by joncham · Jul 07, 2012 at 07:25 PM

Similar to the answer to the original question referenced, you could use the File.ReadAllText method:

 string text = System.IO.File.ReadAllText("myfile.txt");


Comment
Comments Locked · Show 1 · 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 4ndro1d · Nov 21, 2013 at 11:25 AM 0
Share

This only works in Unity Editor but not on the targeted platforms

avatar image
0

Answer by JDyo2 · Jul 25, 2015 at 03:54 PM

It's better do

      using (theReader)
      {
          line = theReader.ReadLine();

          if(line != null){
                  // While there's lines left in the text file, do this:
                  do
                  {
                         // Do whatever you need to do with the text line, it's a string now
                         // In this example, I split it into arguments based on comma
                         // deliniators, then send that array to DoStuff()
                         string[] entries = line.Split(',');
                         if (entries.Length > 0)
                         DoStuff(entries);

                         line = theReader.ReadLine();
                  }
                  while (line != null);
          } 
          // Done reading, close the reader and return true to broadcast success    
          theReader.Close();
          return true;
          }
    }

It uses an if less inside the do-while

Comment
Comments Locked · 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

13 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

Related Questions

Only able to save inside the editor problem 2 Answers

Can't get serialization Load/Save to work!C# 3 Answers

Filestream/SaveManager 2 Answers

Problems with saving/loading score with PlayerPrefs [C#] 1 Answer

Save the inventory items to the database 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