How can I turn text content into integer in C #

0

How can I get a .txt file and transform it into an integer string?

My program has to get the array that is inside the file .txt and put inside an array in the program so that I can manipulate the data inside the array. The part of the file opener I've done, but I'm not finding a way to turn the array into .txt into an array within my program. Please someone help me.

The .txt contains these data:

0 1 0 0 0 0 0

1 0 1 0 0 0 0

0 1 0 1 1 1 0

0 0 1 0 1 1 0

0 0 1 1 1 1 0

0 0 1 1 1 0 0

0 0 0 0 0 1 1
    
asked by anonymous 22.10.2017 / 16:45

1 answer

3

You can split each line of the file with the split by '\ n' and then split each line and put it in your array, for example:

        var matriz = new int[10, 10];
        var texto = File.ReadAllText("Caminho para o arquivo");

        int i = 0, j = 0;
        foreach (var linha in texto.Split('\n'))
        {
            foreach (var letra in linha.Split(' '))
            {
                //verifica se o caracter não é nulo ou vazio
                if (string.IsNullOrWhiteSpace(letra))
                    continue;

                matriz[i, j] = Convert.ToInt32(letra);
                j++;
            }
            i++;
            j = 0;
        }
    
22.10.2017 / 18:58