Hello people! I am making a graph (with chart) that takes information from a text file for the X,Y axes. my problem is when searching in a text file, numbers.
the structure of my txt file is as follows:
int n_survey;string city;int age;string gender;
ex: 1;Buenos Aires;25;Female
2;Corrientes;19;Masculino
3;...
I use Split(';') to skip my separator in that string and my goal is to capture the age of all the records, line by line in the txt file. I have this:
StreamReader stream = File.OpenText(@"C:\Usuarios\...");
List<int> list_edad = new List<int>();
string[] arreglo_cad;
string cad = stream.ReadLine();
while(cad != null)
{
arreglo_cad = cad.Split(';');
list_edad.Add(Convert.ToInt32(arreglo_cad[2]));
// ...[2] porque es la posicion de las edades
cad = stream.ReadLine();
}
foreach(var item in list_edad)
{
if(item >= 16 && item <= 30)
++cont1;
else
++cont2;
}
but the list is not saving the values, any solution?
Try to
File.ReadAllLines()
return all the lines as an array ofstring
:Here done with Linq:
Most likely you are reading the file wrong This is the correct way to read a txt file in c#