-1

I try to take the positions from each line from a txt file and then give them to a tree that has been spawned.I help out in this area: Loads the Coordinates from the save file.

I would like to have it listed in such a way that the 3 digits in each line are all numbers in a multiple ints z.b int1 = first number, int2 = second number, int3 = three nummber.

My txt file looks like this:

-32,68481 1,5 -24,33997;
11,65891 1,5 29,67229;
33,34601 1,5 26,94939;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class SaveSystem : MonoBehaviour
{
                        //Loads the Coordinates from the save file
    public void LoadSaveData()
    {
        string[] data = SaveBaumText.text.Split(new char[] { '\n' });

        for(int i = 0;i < data.Length - 1;i++)
        {
        string[] row = data[i].Split(new char[] { ';' });

        Bäume = GameObject.FindGameObjectsWithTag(BaumTag);

        Debug.Log(row[0]);

        }
    }
  }
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • 1
    Please read [ask] that gives an overview of how to ask a good question. This question is missing details on what problem you are having. It also contains a lot of code without giving us any idea of where to look. It's good to include all relevant code but nothing more. Thanks. – Michael Welch Oct 26 '20 at 21:16
  • @Michael Welch is it now better? – legoeugen Loks Oct 26 '20 at 22:05
  • Well, if it is focused on the right code. Let's start with the first line of `LoadSaveData` what is it doing? What data type is SaveBaumText? I assume whatever it is it has a field named `text` that has the contents of the file, so you are splitting it into lines? Then you are looping thru the lines but it's not clear what you are doing with them. You haven't yet added details about what problem you are having and what you are trying to do. Let's take the first row. `-32,68481 1,5 -24,33997;` What processing do you want to do to it? You just want to parse the string into 3 numbers? – Michael Welch Oct 26 '20 at 22:11
  • yes split in 3 ints.Sorry im not Good in explaining – legoeugen Loks Oct 26 '20 at 22:13
  • Well you can continue to split like you've been doing. Split on white space (after removing the semicolons) and that will give you 3 strings per line. Then you can use `Float.Parse` (or `Double.Parse`) or the `TryParse` version to convert the strings into numbers. – Michael Welch Oct 26 '20 at 22:16
  • Here's an answer that tells you how to split on white space: https://stackoverflow.com/a/6111355/697188 – Michael Welch Oct 26 '20 at 22:17

1 Answers1

0

Based on What's the fastest way to read a text file line-by-line? using a StreamReader to read line by line to minimize memory usage.

private void LoadSaveData()
{
    string fileName = @"c:\test.txt";
    const int BufferSize = 128;
    using (var fileStream = File.OpenRead(fileName))
    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize))
    {
        string line;
        while ((line = streamReader.ReadLine()) != null)
        {
            // Process line
            string[] numberStrings = line.Split( );
            if(numberStrings.Length == 3
                && float.TryParse(numberStrings[0], out float f1)
                && float.TryParse(numberStrings[1], out float f2)
                && float.TryParse(numberStrings[2].TrimEnd(';'), out float f3))
            {
                // do something with your floats
            }
        }
    }
}
Thomas
  • 1,225
  • 7
  • 16