Search Unity

how to read a file??

Discussion in 'Scripting' started by Jackman, Aug 20, 2009.

  1. Jackman

    Jackman

    Joined:
    Apr 20, 2009
    Posts:
    29
    Hi everybody,

    i've search a way to read a file and i've find that :

    Code (csharp):
    1.  
    2. function Read_File (filename : String, tab : Array) {
    3.  
    4.     var SR : StreamReader;
    5.     var S : String;
    6.     var i = 0.0;
    7.     SR=File.OpenText(filename);
    8.     S=SR.ReadLine();
    9.     while(S!=null)
    10.     {
    11.     tab[i] = S;
    12.     i++;
    13.     S=SR.ReadLine();
    14.     }
    15.     SR.Close();
    16.  
    17. }
    18.  
    the file i want to read contains this :

    Code (csharp):
    1.  
    2. 1234
    3. 5678
    4.  
    then i want to compare the 2 numbers but the function "Read_File" returns a string

    so i've tried to do this

    the variable tab contains: tab[0] = "1234" and tab[1] = "5678"
    so tab[0][0] = "1";


    Code (csharp):
    1.  
    2. var trial = 0.0;
    3. if (tab[0][0] == "1"){
    4. trial = 1.0;
    5. }
    6. else{
    7. print("error");
    8. }
    9.  
    and that always prints "error" and i don't understand why...

    i've tried another way, i've thought it could be possiblr*e to convert each character from the file to its ASCII code but unity doesn't seem to recognize the function "charCodeAt()"

    i've tried this :

    Code (csharp):
    1.  
    2. print(tab[0].charCodeAt(1));
    3.  
    and the result is :

    and to finish i've tried to convert the character with "parseInt" but without results... :

    Code (csharp):
    1.  
    2. var code = 0;
    3. print(parseInt(tab[0][1]));
    4.  
    and the result :

    can someboy help me?? (with one of the ideas i had or with another)

    thanks in advance
     
  2. jaxas

    jaxas

    Joined:
    Mar 22, 2009
    Posts:
    59
    i think you need to parse string value to float:
    Code (csharp):
    1.  
    2. function Read_File (filename : String, tab : Array) {
    3.  
    4.    var SR : StreamReader;
    5.     var S : String;
    6.    var i = 0.0;
    7.     SR=File.OpenText(filename);
    8.     S=SR.ReadLine();
    9.     while(S!=null)
    10.     {
    11.    tab[i] =  float.Parse(S); <------------
    12.    i++;
    13.     S=SR.ReadLine();
    14.     }
    15.     SR.Close();
    16.  
    17. }
    18.  
     
  3. Jackman

    Jackman

    Joined:
    Apr 20, 2009
    Posts:
    29
    ok i will try
    thanks for your answer