Search Unity

StreamReader.ReadLine to Array and only display 5 lines at a time c#

Discussion in 'Scripting' started by avidgamer, Aug 3, 2012.

  1. avidgamer

    avidgamer

    Joined:
    Aug 1, 2012
    Posts:
    8
    I have been looking all night but I just cant get the code right, What is the best way to read from a text file, turn it into a array or whatever is best. Then reference that array 5 lines at a time.

    I tried counting the lines and turing that into the arrays index or adding each array in a loop but I cant seem to get it right.

    Heres my code, but you will probably have something better:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;
    5. using System.IO;
    6.  
    7. public class TextGenerator : MonoBehaviour {
    8.    
    9.     protected FileInfo theSourceFile = null;
    10.     protected StreamReader reader = null;
    11.     protected string text = " ";
    12.     //public int linesCount = 0;
    13.     //string[] lines = new string[]{"","","","","","","","","","", };
    14.    
    15.     void Start () {
    16.        theSourceFile = new FileInfo ("Assets/Text/Test.txt");
    17.        reader = theSourceFile.OpenText();
    18.        
    19.         //loop through lines and create array
    20.        
    21.         //count lines but doesnt help read
    22.     //string line;
    23.     //while ((line = reader.ReadLine()) != null)
    24.         //{
    25.             //linesCount++;
    26.         //}
    27.     }
    28.    
    29.     void Update () {
    30.        
    31.         if (Input.anyKey){
    32.         ReadText();
    33.         }
    34.        
    35.     }
    36.     void OnGUI()
    37.     {
    38.         GUI.Label(new Rect(50, 50, 500, 500), text);
    39.  
    40.     }
    41.    
    42.     void ReadText(){
    43.        
    44.         if (text != null ) {
    45.                        
    46.             text +=  reader.ReadLine();
    47.             text += "\n";
    48.            
    49.             // Console.WriteLine(text);
    50.             //print (text);
    51.         }
    52.     }
    53. }
    54.  
    Your help is much appreciated
     
  2. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    so you want the first 5 lines of a file? or you want to display the text from the file like line1-5 then the next bunch of lines that will be displayed are 6-10.
    Code (csharp):
    1.  
    2. void ReadFile(string DataPath){
    3.                 using (StreamReader sr = new StreamReader(DataPath))
    4.             {
    5.            
    6.                 Readfile = new string[File.ReadAllLines(DataPath).Length];
    7.                 String line;
    8.                 int t = 0;
    9.                 // Read and display lines from the file until the end of
    10.                 // the file is reached.
    11.                 while ((line = sr.ReadLine()) != null)
    12.                 {
    13.                     //here you can put something(like an if) that when it hits the 5th line it will stop reading
    14.                     Readfile[t] = line;
    15.                
    16.                     t += 1;
    17.                 }
    18.             }
    19.     }
    20.  
    21.       static string ConvertStringArrayToString(string[] array)
    22.     {
    23.     //
    24.     // Concatenate all the elements into a StringBuilder.
    25.     //
    26.     StringBuilder builder = new StringBuilder();
    27. foreach (string value in array)
    28.     {
    29.         builder.Append(value);
    30.         builder.Append('\n');
    31.     }
    32.     return builder.ToString();
    33.     }
    34.  
    35. text = ConvertStringArrayToString(Readfile);
    36.  
    Something like that?
     
  3. avidgamer

    avidgamer

    Joined:
    Aug 1, 2012
    Posts:
    8
    Hello Falin from the Netherlands,

    Thank you so much for you knowledge,

    yes. And then everytime you press a key it will dispay the next 5 and the next 5 and the next 5 until its null.

    Is it better to store goups of 5 lines in each array element? What would be the best way to loop though the array 5 element at a time? would I still need the StringBuilder?

    Below is the working code dispaying all read line:

    Code (csharp):
    1.  
    2. protected FileInfo theSourceFile = null;
    3. protected StreamReader reader = null;
    4. protected string text = " ";
    5. public string[] Readfile;
    6. int currentLine = 0;
    7.     void Start () {
    8.         ReadFile("Assets/Text/Test.txt");
    9.         text = ConvertStringArrayToString(Readfile);
    10.     }
    11.     void Update () {
    12.         if (Input.anyKeyDown){     
    13.         }
    14.     }
    15.    void OnGUI()
    16.     {
    17.         GUI.Label(new Rect(50, 50, 500, 500), text);
    18.     }
    19.     void ReadFile(string DataPath){
    20.          using (StreamReader sr = new StreamReader(DataPath))
    21.             {
    22.                 Readfile = new string[File.ReadAllLines(DataPath).Length];
    23.                 String line;
    24.                 int t = 0;
    25.                 // Read and display lines from the file until the end of
    26.                 // the file is reached.
    27.                 while ((line = sr.ReadLine()) != null)
    28.                     {
    29.                     //here you can put something(like an if) that when it hits the 5th line it will stop reading           
    30.                     Readfile[t] = line;
    31.                     t += 1;            
    32.                     }
    33.            }
    34.     }
    35.       static string ConvertStringArrayToString(string[] array)
    36.     {
    37.     //
    38.    // Concatenate all the elements into a StringBuilder.
    39.     //
    40.     StringBuilder builder = new StringBuilder();
    41.     foreach (string value in array)
    42.     {
    43.         builder.Append(value);
    44.         builder.Append('\n');
    45.     }
    46.     return builder.ToString();
    47.    }
    48.  
     
  4. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    K i played around with your code and came up with this:
    Code (csharp):
    1. protected FileInfo theSourceFile = null;
    2.     protected StreamReader reader = null;
    3.     protected string text = " ";
    4.     public string[] Readfile;
    5.     static public int LineCount;
    6.     static public int LineCountMax;
    7.     int currentLine = 0;
    8.         void Start () {
    9.             LineCountMax = 4;
    10.             ReadFile("Assets/Text/test.txt");
    11.            // text = ConvertStringArrayToString(Readfile);
    12.         }
    13.         void Update () {
    14.             if (Input.anyKeyDown){  
    15.                 Debug.Log("pressed");
    16.                 text = ConvertStringArrayToString(Readfile);
    17.                 LineCount = LineCountMax;
    18.                 LineCountMax += 5;
    19.             }
    20.         }
    21.        void OnGUI()
    22.         {
    23.             GUI.Label(new Rect(0, 0, 500, 500), text);
    24.         }
    25.         void ReadFile(string DataPath){
    26.              using (StreamReader sr = new StreamReader(DataPath))
    27.                 {
    28.                     Readfile = new string[File.ReadAllLines(DataPath).Length];
    29.                     String line;
    30.                     int t = 0;
    31.                     // Read and display lines from the file until the end of
    32.                     // the file is reached.
    33.                     while ((line = sr.ReadLine()) != null)
    34.                         {
    35.                         //here you can put something(like an if) that when it hits the 5th line it will stop reading          
    36.                         Readfile[t] = line;
    37.                         t += 1;            
    38.                         }
    39.                }
    40.         }
    41.           static string ConvertStringArrayToString(string[] array)
    42.         {
    43.         //
    44.        // Concatenate all the elements into a StringBuilder.
    45.         //
    46.         int i = 0;
    47.              i= LineCount;
    48.         StringBuilder builder = new StringBuilder();
    49.         while(i <= LineCountMax  array[i] != null)
    50.         {
    51.             builder.Append(array[i]);
    52.             builder.Append('\n');
    53.             //Debug.Log("Line:"+i +"LineMax" +LineCountMax);
    54.             i++;
    55.         }
    56.         return builder.ToString();
    57.        }
    maybe it helps
     
  5. avidgamer

    avidgamer

    Joined:
    Aug 1, 2012
    Posts:
    8
    Hello Falin,

    In my Build verion, the string file path doesnt work ofcourse because it can't find the sting "DataPath" that in found in the Editor.

    I tried playing around with:

    TextAsset asset = Resources.Load("something") as TextAsset;
    Stream s = new MemoryStream(asset.bytes);
    BinaryReader br = new BinaryReader(s);

    But I can seem to get it to work. I will keep trying. in the mean time, Any suggestions or work arounds? anyone?
     
  6. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    With the code from above it couldn't find the string DataPath itself or it couldn't find the test.txt file?

    because if it cant find the test.txt:
    You could do it something like this:
    Code (csharp):
    1.     using (StreamReader sr = new StreamReader(Application.dataPath +DataPath))
    2.            
    3.                 Readfile = new string[File.ReadAllLines(Application.dataPath +DataPath).Length];
    then you should remove the "/Assets/" from your path string.
     
  7. smyte100

    smyte100

    Joined:
    Mar 10, 2012
    Posts:
    55
    Hi. Please, i program in javascript and i'm now trying to rewrite a c# script in java so i can use it in my project. I want to ask if you know how it is possible to create a stringBuilder in javascript. I have tried different means and it gives errors like - "Unknown identifier" or "does not denote a valid type".
     
  8. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    There is no standard StringBuilder implemented in JavaScript .
    After searching in google I found this:
    http://www.codeproject.com/Articles/12375/JavaScript-StringBuilder
    Haven't used it myself so dont know if it will work with unity. But you can always try tho.
     
unityunity