Search Unity

Is it possible to read, write, etc. a specific line in csv whilst not using read all lines?

Discussion in 'Editor & General Support' started by XenonSpher, Dec 20, 2016.

  1. XenonSpher

    XenonSpher

    Joined:
    Dec 11, 2015
    Posts:
    29
    is it possible to read, write, insert, replace or delete a line whilst not using read all lines?

    by now you guys could surmise that the reason why I need to do those stuff because I kinda already have a big pile of text in a csv.

    what would practical is maybe to write, insert, replace or delete a text file in a specific Line Number. is that even possible?

    here's my code to read, add, and insert text.. but again... they use a read all line method.

    Code (csharp):
    1.  
    2.  
    3.         string[] lines = System.IO.File.ReadAllLines(CSVUrl);
    4.         string[] temporary;
    5.         string allString = "";
    6.         int tempInt = 0;
    7.         DataIsAlreadyChange = false;
    8.         for (int i = 0; i < lines.Length; i++)
    9.             {
    10.                 LastString = lines[lines.Length-1];                                  
    11.                 temporary = lines[i].Split(new char[] { ',' });
    12.                 if (temporary[0] == this.gameObject.GetComponent<Slider>().SliderID.ToString())
    13.                 {
    14.                 // if we enter here, it means that we found a corresponding ID.so what we'll do now is replace the current data but first we check it if its the last data.
    15.                      if (i == lines.Length - 1)
    16.                         {
    17.                     //since this is the last.. all we got to do is replace the Data
    18.                             lines[i] = DataString;
    19.                             allString += lines[i] + "\n";
    20.                             print("hello");
    21.                         }
    22.                     else
    23.                     {
    24.  
    25.                     //since this is not the last and there are more stuff below the data.  we check how much more is there to put in the all string
    26.                             if (DataIsAlreadyChange == false)
    27.                             {  
    28.                                 ChoosenI = i;    
    29.                                 string[] tempLines = new string[lines.Length - i];
    30.                                 tempLines[tempInt] = DataString;
    31.                                 for (int j = 0; j < lines.Length - i - 1; j++)
    32.                                 {
    33.                                     tempInt++;      
    34.                                     tempLines[tempInt] = lines[i + j + 1];
    35.  
    36.                                 }
    37.                                 tempLast = tempLines;
    38.                                 DataIsAlreadyChange = true;
    39.                             }
    40.                     }
    41.                 }
    42.                 else
    43.                 {
    44.                     //if we enter here, it means that we did not find a corresponding ID or there is none. so we'll just copy the line and place it in allString or add a line
    45.                        // 7       //8-1=7
    46.                     if(i == (lines.Length - 1))
    47.                     {
    48.                     //this is the very last line and if we got here without any corresponding ID we add a line
    49.                             if(DataIsAlreadyChange == true)
    50.                             {
    51.                                 for(int k = 0; k < lines.Length - ChoosenI;k++)
    52.                                 {
    53.                                     lines[ChoosenI+k] = tempLast[k];
    54.                                    allString += lines[ChoosenI + k] + "\n";
    55.                                 }
    56.                             }
    57.                         else
    58.                              Addline();
    59.                             AddLineIsActicated = true;
    60.  
    61.                     }
    62.                     else
    63.                     {
    64.                         //no correspondning ID and not the last line so we want we do is copy it to the allstring.
    65.                         //basically this place is where we write the names. like the ID, posX,posY, etc. and the data's that we skip
    66.                             if(DataIsAlreadyChange == false)
    67.                             {allString += lines[i] + "\n";}
    68.                     }
    69.                 }
    70.             }
    71.         if (AddLineIsActicated == false || DataIsAlreadyChange == true)
    72.         {
    73.             System.IO.File.WriteAllText(CSVUrl, allString);
    74.         }
    75.  
    76.  
    77.     void Addline()
    78.     {
    79.         string[] lines = System.IO.File.ReadAllLines(CSVUrl);
    80.         string li = lines[0];
    81.         using (StreamWriter sw = File.AppendText(CSVUrl))
    82.         {
    83.             if (lines[0] != null)
    84.             {
    85.                 sw.WriteLine(DataString);
    86.             }
    87.         }
    88.     }
    89.