How to use Streamreader to read the.txt file and insert the data into a List?

Asked

Viewed 214 times

-1

I created a program that performs CRUD , whenever I create, update or delete an employee it updates the Txt file using Streamwriter, when starting the program I am trying to read that same file and already insert in the List I created, to be able to have the data from when I used it earlier, however, the way I did the Streamreader it is not putting in the List correctly, despite reading all the lines.

 List<Funcionarios> func = new List<Funcionarios>();
            using (StreamReader sr = new StreamReader(@"C:\Users\Usuário\Teste.txt"))
            {
                string line;
                Funcionarios funcionario = new Funcionarios();
                while ((line = sr.ReadLine()) != null)
                {
                    funcionario.nome = sr.ReadLine();
                    funcionario.matricula = sr.ReadLine();
                    funcionario.cargo = sr.ReadLine();
                    func.Add(funcionario);
                }

There is the part of the code that reads the file and should store in List func.

  • tries to instantiate the funcionario within the while instruction

  • It wasn’t yet, when I saved something inside the file. txt and choose the option to List in the program appears only the position and the employee registration, oq n makes sense already q I put 3 information and in the code I am reading the three..

  • What exactly do you mean, "he’s not putting it in the list correctly". Present an example of your text file... Instead of putting each information on a line, wouldn’t it be more appropriate to write one line per employee and separate the values by some marker? ex.: ;

  • 1

    it makes no sense for you to use this form of writing in the document (because sr.readline() is also being a search parameter). Ideally you would register an entire row using specific tabs (string1 - string2 - string3). And when using readline(), make a split to place these strings in each parameter of the object

  • http://prntscr.com/p0twqb. This was the official I saved. http://prntscr.com/p0tx1m he was saved this way in the archive. txt but when opening the program again and asking it to list, he listed this: http://prntscr.com/p0txfj

  • Walter how would I do it this way? Using the split? I switched to put all the employee data on one line, and separated the data by ";" I just couldn’t get the Streamreader to store on each object

  • @Furiosg Edit the question and present your updated code

  • Thank you so much for your help, I researched Split and got my code the way I wanted it.

Show 3 more comments

1 answer

1

Your main mistake is reading a line for each assignment you are doing. Every time you run the sr.ReadLine();, is reading and moving the "cursor" to the next line.

Even in your original scenario where each value is in a row, when you make the while statement ((line = sr.ReadLine()) != null), is already reading, storing and directing to the next line... so when executing the code snippet within the loop you are not working at the expected interval.

Taking into consideration the following structure of your text file as in the example below:

Nome do Primeiro Funcionário;0001;Diretor
Nome do Segundo Funcionário;0002;Gerente
Nome do Terceiro Funcionário;0003;Operador
Nome do Quarto Funcionário;0004;Auxiliar

You can read each line and use the split to set the value for each of the properties.

List<Funcionario> listaFuncionarios = new List<Funcionario>();
using (StreamReader sr = new StreamReader(@"C:\Temp\Funcionarios.txt"))
{
    string line;
    while ((line = sr.ReadLine()) != null)
    {
        var dadosFuncionario = line.Split(';');
        Funcionario funcionario = new Funcionario
        {
            Nome = dadosFuncionario[0],
            Matricula = dadosFuncionario[1],                        
            Cargo = dadosFuncionario[2]
        };

        listaFuncionarios.Add(funcionario);
    }
}
  • @furiosg it became clear where you were going wrong?

  • Yes, thank you very much, I had already used the split and it worked right!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.