I understand your logic. You want to store text value information for three-element vectors. I just don’t understand if you want to do this to generate a single vector or want to store multiple vectors, but surely both cases are possible.
You just need to define a logic for your text. If you store an immutable fixed number of vectors of three numbers, you can store everything in a 3x matrixn, where n is the number of column vectors to be stored (in this case as specified as the first number in the text file). I call it "matrix" for its concept of linear algebra, but it is nothing more than a array vector, so it will be a vector type[n], as apparently is what you want.
Let’s assume that you want to store everything in an array. In this implementation, what we would do is:
- Read the first line to define the dimensions of a matrix (array of vectors);
- Read each row of three numbers divided by spaces;
- Divide each string between its spaces;
- Fill in the template with strings obtained by converting them to float or double (depending on the accuracy you want).
Below is the detailed implementation:
using System;
using System.Linq;
namespace Programa
{
public class Program
{
Vector3[] Load(string filePath) // Endereço do arquivo
{
Vector3[] v;
using (StreamReader sr = new StreamReader(filePath))
{
string[] linha;
float[] nrs = new float[3];
int size = int.Parse(sr.ReadLine()); // Leio a primeira linha apenas, com o número de vetores da matriz
int i = 0;
v = new Vector3[size];
while (!sr.EndOfStream && i < size)
{
linha = sr.ReadLine().Split(' '); // Divido pelos espaços
// Veja as outras sobrecargas de Split para mais opções
nrs = linha.Select(n => float.Parse(n)).ToArray(); // Uso Linq para selecionar cada elemento e converter para float
v[i] = new Vector3 (nrs[0], nrs[1], nrs[2]);
i++;
}
}
return v;
}
}
}
Instead of float. Parse, use double.Parse for double decimal precision if desired. Also, always recommend (!!) using the blocks using as I used now, as they ensure that the Streamreader will be arranged after use.
(Note: I haven’t tested the algorithm yet, I’m on Linux at the moment. Of course, I edit later if there are any problems)
I don’t understand. What do you intend to do?
– Jéf Bueno
Put in more parts of the code, what is this
entrada
?– Maniero
You could use a readline and then split by space.
– Hiro
I edited and entered the complete code
– André Carvalho