• 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 /
avatar image
Question by EnderDragoon · Mar 19, 2015 at 12:16 PM · c#text file

Read last line of .txt file to float variables

I have a program that outputs a stream of variables in a txt file. I need to build an interface in Unity. Most other stuff I have well sorted out but breaking apart a large txt file in C# I haven't messed with. Most methods record the entire body of text to a variable, stream or array but I only need the last few lines. The text looks like t$$anonymous$$s:

  54.41316 |      54.90971 |      60.67264 |      60.67264 |      62.61755 |      69.82083 |      69.82083 |
  54.39159 |      54.88930 |      60.65312 |      60.65312 |      62.59273 |      69.79837 |      69.79837 |
  54.37241 |      54.86911 |      60.63384 |      60.63384 |      62.

You can see the last few numbers are missing on the last line because if the file is opened at an

Comment

People who like this

0 Show 4
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 Eluate · Mar 19, 2015 at 10:15 AM 0
Share

@EnderDragoon this may not be the most efficient way into what you are trying to achieve. Why not use JSON?

avatar image EnderDragoon · Mar 19, 2015 at 10:28 PM 0
Share

Not familiar with JSON. I'll start reading about it. Does it have the capacity to do this? What's happening is X-Plane 10 outputs a Data.txt file that's updated several times per second with flight data. I'm attempting to build aircraft gauges (airspeed, altimeter, turn coordinator, etc) that read the txt file for the telemetry to animate the gauges. I can also output the data in a UDP stream to an IP address (which can be local host) but I have very little experience with network coding and interpreting a UDP stream.

avatar image Eluate · Mar 20, 2015 at 05:34 AM 0
Share

If you are using a JSONObject library, you can access the JSONObject like an array, for example jsonObject[1].

avatar image spiceboy9994 · Mar 21, 2015 at 12:22 AM 0
Share

In my experience Json objects are used for that exactly, for OBJECT representation. In your case, if you don't have any security restrictions, your txt file is just fine. However, I don't get the part where you mention "the last few lines"? you can get the last x numbers if you parse the file within a stream and by simple split, you can get the array:

 var var fileStream = new StreamReader("FILEPATH");
 string x = fileStream.ReadToEnd(); // suposing its the object with your file
 string[] fileLines = x.Split('|');
 //Now that you have the array you can access the data by index

 //Or you can find an especific object using linq if you need
 
 var matchingLines = fileLines.ToList().Where(l=> l.Contains("60.")).ToList();

Regards

BTW, you don't need the spacing on the txt (in case that it's something you control). You can have all the data trimmed, just ensure you have a delimitator.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

NullReferenceException when trying to programatically populate cubes 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